/** ***********************************************************************************
* @file         mangpatio.c
* @brief    	国标808协议管理
* @details      主要用来处理808协议内容
* @author       
* @date         2021/5/27
* @copyright    北京高知科技有限公司    
* @version      V1.0
* @attention    无
**************************************************************************************/
#define OSI_LOG_TAG OSI_MAKE_LOG_TAG('8', '0', '0', 'C')

#include "fibo_opencpu.h"
#include "algorithm.h"
#include "files.h"
#include "net_port.h"
#include "gaoz_api.h"
#include "gnss_task.h"
#include "national_data_process.h"
#include "vehicle_data_process.h"
#include "com_lib.h"
#include "fota.h"
#include "bluetooth_app.h"
#include "flash_config.h"
#include "flash_dev.h"
#include "mangpatio.h"
#include "qxwz_service.h"
#include "AES.h"
#include "event_task.h"
#define MANG_SENDTASK_NAME   "mangpat_sendtask"
#define MANG_RECVTASK_NAME   "mangpat_recvtask" 

static uint8_t	mangpatio_reset_reason = 0;
static uint8_t	mangpatio_auth_reason = 0; 

uint8_t terid[6]={0};  								/*808终端ID*/
uint16_t mangpatioserialnum=0;  					/*808流水号*/
static uint32_t mangpatio_auth_recv_st = 0;
static uint32_t mangpatio_auth_fail_times = 0;
net_reset_e  mangpatio_rst = NET_RST_INT;  			/*修改网络参数重启网络*/
mangpatio_info_t	terinf = {0},currinf = {0};		/*终端参数*/
//terminal_ota_t terota_handle = {0};				/*终端升级操作句柄*/
socket_handle_s socket_handle_2;  					/*定义socket handle 可以同时打开6路通道*/
socket_state_t socket_state_2;   					/*定义socket 网络状态*/
GaozSemaphoreHandle_t   mangpatio_socket_semaphore = 0;	/*网络状态信号量*/
GaozSemaphoreHandle_t   mangpatio_recv_data_mutex = 0; 	/*接收互斥*/
GaozSemaphoreHandle_t   majorpara_table_mutex = 0; 		/*操作参数表互斥*/
national_run_state_e mangpatio_runing_st;   		/*网络运行的状态*/
national808_netready_e mangpatio_netready;    		/*休眠唤醒后网络状态*/
national_response_e mangpatio_response;     		/*平台应答的方式*/
national_send_data_e mangpatio_send_data;   		/*平台发送数据的状态*/
national_net_data_st_t mangpatio_net_data; 			/*发送接收数据的网络状态*/
GaozQueueHandle_t mangpatio_send_patiores_queue; 	/*发送平台数据应答消息队列*/
GaozQueueHandle_t mangpatio_send_devres_queue; 		/*发送设备应答消息队列*/
GaozQueueHandle_t mangpatio_ctlock_queue;    		/*控锁消息队列*/
GaozQueueHandle_t mangpatio_ctlock_res_queue;    	/*控锁异步消息队列*/
GaozQueueHandle_t mangpatio_upgrade_queue; 			/*升級消息队列*/
national808_ctrllock_e mangpatio_ctlock;   			/*控锁状态*/
static uint32_t mangpatio_set_time_tick;    		/*周期上报时间*/
static uint32_t mangpatio_set_meter_tick;     		/*周期上报距离*/
static uint32_t mangpatio_herat_time_tick;     		/*心跳上报时间*/
static mangpatio_sleep_wakeup_state_e mangpatio_sleep_wakeup_st;/*系统唤醒源状态*/

uint8_t mangpatio_broken_link;						/*主动断开网络连接状态，1-主动断开 0-初始化状态*/

#define majorpara_mutex_lock()  gaoz_mutex_lock(majorpara_table_mutex)  
#define majorpara_mutex_unlock()  gaoz_mutex_unlock(majorpara_table_mutex)

national808_data_st_t mangpatio_data_st; /*发送数据缓存的状态*/
static n808_patio_recv_buf_t mangpatio_recv_data;  /*数据接收平台缓存数据*/
static uint32_t mangpatio_min_mile=0,mangpatio_allkilo_mod=0;
static void mangpatio_socket_semaphore_create(void);
static void mangpatio_socket_semaphore_put(void);
static uint8_t mangpatio_socket_semaphore_get( uint32_t wait_time );
static void mangpatio_create_network_state_cb( void * pv);
static void mangpatio_net_st_check_task(void *param);
static void mangpatio_data_analytic_fun( national_net_res_msg_t *net_res);
//void mangpatio_run_state_set( national_run_state_e  state );
static uint32_t mangpatio_socket_send_data_suc( socket_state_t *state ,uint32_t time );
static uint32_t parse_mangpatio_recv_data( uint8_t *da, uint16_t len );
static int mangpatio_saveserviceloadpara(uint8_t *pDa,uint8_t type,uint8_t *pDa2);
static uint8_t mangpatio_setParafromservice(uint8_t * da, uint16_t len);
static int mangpatio_readTerminalPara(const INT8 *filename);
static uint8_t mangpatio_initTerminalParaAll(uint8_t * ManageID,uint8_t *da,uint16_t *len);
static uint16_t mangpatio_SendRegister(uint8_t *da);
static uint16_t mangpatio_Authentication(uint8_t *da);
static uint16_t mangpatio_SendHeart(uint8_t *da);
static uint16_t mangpatio_positioninfo(uint8_t sel,uint8_t *da);
static uint16_t mangpatio_generalrespond(national808_net_devres_msg_t *res_msg,uint8_t *da);
static uint16_t mangpatio_rsarespond(national808_net_devres_msg_t *res_msg,uint8_t *da);
static uint16_t mangpatio_lockrespond(uint8_t *da, national808_ctrllock_msg_t *res_msg);
static uint16_t mangpatio_property(uint8_t *da);
static uint16_t mangpatio_parameter(national808_net_devres_msg_t *res_msg,uint8_t *da);
static uint16_t mangpatio_msgtranslation(uint8_t *da,uint8_t *txbuf, uint16_t len);
static uint16_t mangpatio_updateresult(update_result_t *upslt,uint8_t *da);
void mangpatio_run_state_set( national_run_state_e  state );
void mangpatio_ctrllock_state_set( national808_ctrllock_e  state );
static uint16_t qxwzrespond(national808_net_devres_msg_t *res_msg,uint8_t *da);

static void mangpatio_net_control_state_check( void );

static uint32_t mangpatio_decrypt_package( uint8_t * pDa, uint16_t *len, uint16_t serailNum );
#if( MANGPATIO_USER_AES_ENCRYPT == 1 )
static uint32_t mangpatio_encrypt_package( uint8_t *pDa, uint16_t *len, uint16_t serailNum );
#endif
static uint32_t mangpatio_decrypt_package_shell( uint8_t* pDa, uint16_t* len );
#if( MANGPATIO_USER_AES_ENCRYPT == 1 )
static uint32_t mangpatio_encrypt_package_shell( uint8_t* pDa, uint16_t* len );
#endif

/** ****************************************************************************
* @remarks       void mangpatio_recv_data_mutex_create( void )
* @brief         接收平台数据互斥体
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     在系统启动后进行创建
*******************************************************************************/
static void mangpatio_recv_data_mutex_create( void )
{
    /*创建任务互斥体*/
    mangpatio_recv_data_mutex = gaoz_mutex_create();
    if( mangpatio_recv_data_mutex == 0 )
    {
        while(1); /*创建失败*/
    }
}
/** ****************************************************************************
* @remarks       static void mangpatio_socket_semaphore_create(void)
* @brief         网络状态信号创建
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void mangpatio_socket_semaphore_create(void)
{
    mangpatio_socket_semaphore = gaoz_sem_new(0);
    if( mangpatio_socket_semaphore == 0 )
    {
        while(1);
    }
}

/** ****************************************************************************
* @remarks       static uint8_t mangpatio_socket_semaphore_get( uint32_t wait_time )
* @brief         获取网络回调状态的信号量
* @param[in]     wait_time 等待信号的时间 单位S
* @param[out]    无
* @return        1 等待信号成功  ， 0 等待信号失败，超时等待
* @attention     无
*******************************************************************************/
static uint8_t mangpatio_socket_semaphore_get( uint32_t wait_time )
{
    if(mangpatio_socket_semaphore == 0)
    {
        mangpatio_socket_semaphore_create();
    }
    if( gaoz_sem_try_wait( mangpatio_socket_semaphore, wait_time ) == true ) //等待回应信号量
    {
        return 1;
    }
    else
    {   
        return 0;
    }
}

/** ****************************************************************************
* @remarks       void mangpatio_socket_semaphore_put(void)
* @brief         释放信号
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void mangpatio_socket_semaphore_put(void)
{
    if( mangpatio_socket_semaphore != 0 )
    {
        gaoz_sem_signal( mangpatio_socket_semaphore );
    }
}

/** ****************************************************************************
* @remarks       void mangpatio_socket_state_set(create_socket_state_e sta)
* @brief         网络状态
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void mangpatio_socket_state_set(create_socket_state_e sta)
{
    socket_state_2.create_socket_state = sta;
}

/** ****************************************************************************
* @remarks       create_socket_state_e mangpatio_socket_state_get( void )
* @brief         获取网络状态
* @param[in]     无
* @param[out]    无
* @return        网络连接状态
* @attention     2022.05.09 YXX ，修改主网络贴换时关闭辅助网络 连接
*******************************************************************************/
create_socket_state_e mangpatio_socket_state_get( void )
{
	return socket_state_2.create_socket_state;
}

/** ****************************************************************************
* @remarks       void mangpatio_sleep_wakeup_state_set( mangpatio_sleep_wakeup_state_e st )
* @brief         辅助平台休眠唤醒状态设置
* @param[in]     st 休眠唤醒状态
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void mangpatio_sleep_wakeup_state_set( mangpatio_sleep_wakeup_state_e st )
{
	mangpatio_sleep_wakeup_st = st;
}

/** ****************************************************************************
* @remarks       mangpatio_sleep_wakeup_state_e mangpatio_sleep_wakeup_state_get( void )
* @brief         获取辅助平台休眠唤醒状态
* @param[in]     无
* @param[out]    无
* @return        休眠唤醒状态
* @attention     无
*******************************************************************************/
mangpatio_sleep_wakeup_state_e mangpatio_sleep_wakeup_state_get( void )
{
	return mangpatio_sleep_wakeup_st;
}

/** ****************************************************************************
* @remarks       uint32_t mangpatio_socket_send_data_suc( socket_state_t * state ,uint32_t time )
* @brief         等待发送数据成功状态
* @param[in]     *state 网络状态 数据地址
* @param[in]     time 等待的时间
* @param[out]    无
* @return        pdPASS，pdFAIL
* @attention     无
*******************************************************************************/
static uint32_t mangpatio_socket_send_data_suc( socket_state_t *state ,uint32_t time )
{
	uint32_t ret = pdFAIL;
    if( mangpatio_socket_semaphore_get( time ) == 1 )
    {
    	/******************判断网络数据是否发生完成****************************/
    	if( state->network_run_state.send_st == SEND_OK )
    	{
            state->network_run_state.send_st = 0;
    		ret = pdPASS;
    	}
    }
	return ret;
}


/** ****************************************************************************
* @remarks       void mangpatio_auth_reason_set(uint8_t reason)
* @brief         鉴权原因设置
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void mangpatio_auth_reason_set(uint8_t reason)
{
	mangpatio_auth_reason  = reason ;
}

/** ****************************************************************************
* @remarks       uint8_t mangpatio_auth_reason_get(void)
* @brief         鉴权原因获取
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
uint8_t mangpatio_auth_reason_get(void)
{
	return mangpatio_auth_reason;
}

/** ****************************************************************************
* @remarks       uint8_t mangpatio_auth_reason_clear(void)
* @brief         清除鉴权重启原因
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
uint8_t mangpatio_auth_reason_clear(void)
{
	mangpatio_auth_reason_set(0);

	if(mangpatio_reset_reason)
	{
		mangpatio_reset_reason = 0;
		reset_reason_clear();
	}
}

/** ****************************************************************************
* @remarks       static uint32_t mangpatio_create_network_state_cb( uint8_t n , uint8_t *arg )
* @brief         NET网络状态回调函数
* @param[in]     *pv 不定类型的函数入参 
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void mangpatio_create_network_state_cb( void *pv)
{

	socket_net_state_s *net_state = NULL;
    socket_state_t *state = NULL;
	if( pv == NULL )				/*空指针返回*/
	{
		return;
	}
	net_state = ( socket_net_state_s * )pv;
    state = &socket_state_2;
    

	switch( net_state->net_state )
	{
	    case SOCKET_STATE_INIT:
            /*无*/
            break;
		case SOCKET_SEND_OK:		/*数据发送成功*/
			state->network_run_state.send_st = SEND_OK;
			mangpatio_socket_semaphore_put(); /*发送信号量*/
			break;
		case SOCKET_SEND_ERROR:		/*数据发送失败*/
			state->network_run_state.send_st = SEND_ERR;
			mangpatio_socket_semaphore_put(); /*发送信号量*/
			break;
		case SOCKET_RECV_OK:		/*数据接收成功*/
			state->network_run_state.recv_st = RECV_OK;
			break;
		case SOCKET_RECV_ERROR:		/*数据接收失败*/
			state->network_run_state.recv_st = RECV_ERR;
			break;
		case SOCKET_LINK_ERROR:		/*网络连接失败*/
			state->network_run_state.link_st = LINK_ERR;
			break;
		case SOCKET_CSQ_ERROR:		/*网络信息状态异常*/
			state->network_run_state.csq_st = CSQ_ERR;
			break;
		case SOCKET_REG_ERROR:		/*网络附着网络失败*/
			state->network_run_state.reg_st = REG_ERR;
			break;
		case GSM_NET_SLEEP:			/*底层网络睡眠*/
			state->network_run_state.net_st = NET_SLEEP;
			break;
		case GSM_NET_STOP:			/*底层网络停止*/
			state->network_run_state.net_st = NET_STOP;
			break;
		case GSM_NET_RST:			/*底层网络需要复位*/
			state->network_run_state.net_st = NET_RST;
			break;
		case GSM_NET_RUN:			/* 底层网络运行中*/
			state->network_run_state.net_st = NET_RUN;
			break;
	}
}
/** ****************************************************************************
* @remarks       static uint32_t mangpatio_net_switch_st_check(mangpatio_info_t  *_info)
* @brief         判断参数表模式字段是否满足联网要求
* @param[in]     minor_info维护表
* @param[in]    
* @param[out]    无
* @return        pdFAIL 网络关  pdPASS 网络开
* @attention     
*******************************************************************************/
static uint32_t mangpatio_net_switch_st_check(mangpatio_info_t  *minor_info)
{
	uint32_t ret = pdFAIL;
	if( (minor_info == NULL ) )
	{
		return ( ret );
	}

	if( (minor_info->Mode == 0x10) || (minor_info->Mode == 0x20) )
	{
		ret = pdPASS;
	}
	else 
	{
		ret = pdFAIL;
	}

	return ret;
}
/** ****************************************************************************
* @remarks      static uint32_t get_twonetipdomain_equal( void )
* @brief         判断主副域名是否相同满足联网要求
* @param[in]     主参数表，副参数表
* @param[in]    
* @param[out]    无
* @return        pdFAIL 相同  pdPASS 不同
* @attention     
*******************************************************************************/
static uint32_t get_twonetipdomain_equal(terminal_info_t *major_info,  mangpatio_info_t  *minor_info )
{
	uint32_t ret = pdFAIL;
	char getip[64] = {0};

	if( (minor_info == NULL ) || (major_info == NULL))
	{
		return ( ret );
	}
	
	if( (memcmp(major_info->IP1,minor_info->IP1,major_info->IP1Len) != 0) ||
		(memcmp(major_info->Port1,minor_info->Port1,major_info->Port1Len) != 0))
	{
		if(connect_type_check((uint8_t *)major_info->IP1) == CONNECT_TYPE_DOMAIN)
		{
			ret = pdPASS;
		}
		else
		{
			ret = gaoz_socket_dns_ip_addr_get(minor_info->IP1,getip);
			if(ret == pdPASS)
			{
				if(memcmp(major_info->IP1,getip,major_info->IP1Len) == 0)
				{
					ret = pdFAIL; 
				}
			}
			else
			{
				ret = pdFAIL; 
			}
		}
	}
	else 
	{
		ret = pdFAIL;
	}
	return ret;
}

/** ****************************************************************************
* @remarks       static uint32_t national_net_create_st_check(socket_handle_s * handle , socket_state_t * state)
* @brief         创建 SOCKET 连接检测状态
* @param[in]     *handle 网络设备节点
* @param[in]     *state 网络状态
* @param[out]    无
* @return        pdFAIL 网络异常 pdFAIL 网络正常
* @attention     进入网络连接成功后分别对网络状态进行检测，其中1 2 3 4 5
			    代表检测的先后次序，同时也代表了事件检测的有限等级。
			    1、网络发送接收数据状态判断，超出设定次数后进行网络关闭；
			    2、网络链接异常，底层驱动设置。网络失效断开链接
			    3、网络信号状态异常，底层驱动设置。等待网络信号恢复正常但是不关闭网络链接。(链接超时后底层驱动会通知 2 )
			    4、网络附着状态异常，底层驱动设置。网络失效断开链接
			    5、底层网络运行状态设置，底层驱动设置。网络失效断开链接
*******************************************************************************/
static uint32_t mangpatio_net_create_st_check(socket_handle_s *handle , socket_state_t *state, char *ip, uint16_t port)
{
	uint32_t ret = pdFAIL;
	if(( handle == NULL ) || ( state == NULL ))
	{
		return ( ret );
	}
	if( state->create_socket_state  == SOCKET_LINK_CREATE_ERROR )
	{	
		memset((uint8_t *)state , 0x00 , sizeof(socket_state_t) );
		if((mangpatio_net_switch_st_check(&terinf) == pdPASS) && (get_twonetipdomain_equal(&taginf,&terinf) == pdPASS))
		{
			gaoz_power_man_wakeup_task_num_run(); /*wake up*/ 
			//统一用主网的车辆id;
			if((strlen((char *)taginf.ManageID) != 0) &&
			   memcmp(terinf.ManageID,taginf.ManageID,sizeof(taginf.ManageID) != 0))
			{
				memcpy(terinf.ManageID,taginf.ManageID,sizeof(taginf.ManageID));
			}
			gaoz_socket_bind( (socket_handle_s*)handle, ip, port, e_socket_protocol_tcp, mangpatio_create_network_state_cb );
			ret = gaoz_socket_connect( (socket_handle_s*)handle );
			if( ret == pdPASS )
			{
				log_debug_printf(INFOR_MODE,"800c" ,"ip debug mangpation ip:%s,port:%d,terinf:%x\r\n",ip,port,terinf.Mode);
				//log_debug_send(INFOR_MODE,"808_" ,"Registered platform network is successful\r\n",0);
				mangpatio_run_state_set( TER_RUNING_REG );
				state->create_socket_state = SOCKET_LINK_CREATE_OK;
				//mangpatio_data_st.send_st = N808_SEND_INIT;
				mangpatio_ctrllock_state_set( CTLOCK_OK );
				gaoz_task_sleep( 1000 );	/*系统延迟1S钟等待网络稳定*/
			}
			gaoz_power_man_wakeup_task_num_stop();
		}	
		else
		{
			ret = pdFAIL;
		}
	}
	else if( state->create_socket_state == SOCKET_LINK_CREATE_OK)
	{
        /*判断数据收发状态是否等待状态异常*/
/*1*/   if(( mangpatio_net_data.send_st >= NET_DATA_SEND_RECV_COUNTER ) || ( mangpatio_net_data.recv_st >= NET_DATA_SEND_RECV_COUNTER ))
        {
			log_debug_printf(INFOR_MODE,"800c" ,"socket_close %d,%d\r\n",mangpatio_net_data.send_st,mangpatio_net_data.recv_st);
            socket_close((socket_handle_s*)handle);
            state->create_socket_state = SOCKET_LINK_CREATE_ERROR;
			state->network_run_state.link_st = 0x00;
           
            ret = pdFAIL;

			if( mangpatio_net_data.send_st >= NET_DATA_SEND_RECV_COUNTER )
			{
				mangpatio_auth_reason_set(NET_AUTH_REASON_SEND_ERR);
			}
			else 
			{
				mangpatio_auth_reason_set(NET_AUTH_REASON_RECV_ERR);
			}

			 mangpatio_net_data.send_st = 0;
            mangpatio_net_data.recv_st = 0;
        }
        /*判断网络模块网络连接状态异常*/
/*2*/	else if( state->network_run_state.link_st == LINK_ERR )
		{
			log_debug_printf(INFOR_MODE,"800c" ,"socket_close link_st%d\r\n",state->network_run_state.link_st);
			gaoz_socket_close((socket_handle_s*)handle);
			state->create_socket_state = SOCKET_LINK_CREATE_ERROR;
			state->network_run_state.link_st = 0x00;
			ret = pdFAIL;
			mangpatio_auth_reason_set(NET_AUTH_REASON_LINK_ERR);
		}
		/*信号质量异常网络注册网络节点不关闭等待网络信号回复正常*/
/*3*/	else if( state->network_run_state.csq_st == CSQ_ERR )
		{
			state->network_run_state.csq_st = 0x00;
			ret = pdFAIL;
			mangpatio_auth_reason_set(NET_AUTH_REASON_CSQ_ERR);
		}
		/*判断GSM模块网络注册状态异常*/
/*4*/	else if( state->network_run_state.reg_st == REG_ERR )
		{
			log_debug_printf(INFOR_MODE,"800c" ,"socket_close reg_st%d\r\n",state->network_run_state.reg_st);
			gaoz_socket_close((socket_handle_s*)handle);
			state->create_socket_state = SOCKET_LINK_CREATE_ERROR;
			state->network_run_state.reg_st = 0x00;
			ret = pdFAIL;
			mangpatio_auth_reason_set(NET_AUTH_REASON_REG_ERR);
		}
		/*网络进入 (睡眠 / 停止 / 复位 / 运行) 的状态*/
/*5*/	else if( state->network_run_state.net_st != 0 )
		{
			if((state->network_run_state.net_st == NET_SLEEP)||(state->network_run_state.net_st == NET_STOP))
			{
				state->network_run_state.net_st = 0;
				//if(hex4CombineOne(terinf.heartBeatDly) != 0)
				//{
				//	gaoz_system_wakeup_rtc_time_set(RTC_NET,hex4CombineOne(terinf.heartBeatDly));
				//}
				/*网络进入休眠时不需要通知应用程序，屏蔽掉网络休眠标志位。YXX 2022.07.25*/
				//gaoz_log_debug_printf(INFOR_MODE,"800c","sleep gaoz_system_wakeup_rtc_time_set : %d\r\n",hex4CombineOne(terinf.heartBeatDly));	
				//if(mangpatio_netready != NET_READY_FAIL)
				//{
				//	mangpatio_netready = NET_READY_FAIL;
				//}
				ret = pdFAIL;
			}
			else if(state->network_run_state.net_st == NET_RUN)
			{
				state->network_run_state.net_st = 0;
				/*网络进入休眠时不需要通知应用程序，屏蔽掉网络休眠标志位。YXX 2022.07.25*/
				//if(mangpatio_netready != NET_READY_OK)
				//{
				//	mangpatio_netready = NET_READY_OK;
				//}	
				ret = pdPASS;
			} 
		}
		else if( mangpatio_broken_link == 1 )
		{
			mangpatio_saveserviceloadpara(NULL,2,NULL);/*2022.05.31 YXX 当主平台关闭时，辅平台需要将鉴权信息进行清空，防止下次鉴权码错误*/
			log_debug_printf(INFOR_MODE,"800c" ,"socket_close broken_link == 1\r\n");
			gaoz_socket_close((socket_handle_s*)handle);
			state->create_socket_state = SOCKET_LINK_CREATE_ERROR;
			mangpatio_broken_link = 0;
			ret = pdFAIL;
		}
/*6*/	else
		{
			ret = pdPASS;
		}
	}
	else
	{
		ret = pdFAIL;
	}
	return ( ret );
}

/** ****************************************************************************
* @remarks       national_run_state_e national_net_runing_state_get( void )
* @brief         获取国标平台运行状态
* @param[in]     无
* @param[out]    无
* @return        运行状态
* @attention     在系统启动后进行创建
*******************************************************************************/
national_run_state_e mangpatio_run_state_get( void )
{
	return mangpatio_runing_st;
}

/** ****************************************************************************
* @remarks       void mangpatio_run_state_set( national_run_state_e  state )
* @brief         设置国标平台运行状态
* @param[in]     state 运行状态
* @param[out]    无
* @return        无
* @attention     在系统启动后进行创建
*******************************************************************************/
void mangpatio_run_state_set( national_run_state_e  state )
{
	mangpatio_runing_st = state;
}
/** ****************************************************************************
* @remarks       mangpatio_ctrllock_e mangpatio_ctrllock_state_get( void )
* @brief         控锁状态
* @param[in]     无
* @param[out]    无
* @return        运行状态
* @attention     在系统启动后进行创建
*******************************************************************************/
national808_ctrllock_e mangpatio_ctrllock_state_get( void )
{
	return mangpatio_ctlock;
}

/** ****************************************************************************
* @remarks       void mangpatio_ctrllock_state_set( national808_ctrllock_e  state )
* @brief         设置控锁状态
* @param[in]     state 运行状态
* @param[out]    无
* @return        无
* @attention     在系统启动后进行创建
*******************************************************************************/
void mangpatio_ctrllock_state_set( national808_ctrllock_e  state )
{
	mangpatio_ctlock = state;
}
/** ****************************************************************************
* @remarks       static void national808_net_patiores_queue_create( void )
* @brief         国标数据平台接收命令应答消息队列创建
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void mangpatio_net_patiores_queue_create( void )
{
    mangpatio_send_patiores_queue = gaoz_queue_create( NET_ACK_QUEUE_MAX_NUM, sizeof( national_net_res_msg_t ) );
    if( mangpatio_send_patiores_queue == 0 )
    {
        while( 1 );
    }
}
/** ****************************************************************************
* @remarks       static void mangpatio_net_devres_queue_create( void )
* @brief         国标数据平台接收数据应答消息队列创建
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void mangpatio_net_devres_queue_create( void )
{
	mangpatio_send_devres_queue = gaoz_queue_create( NET_ACK_QUEUE_MAX_NUM, sizeof( national808_net_devres_msg_t ) );
    if( mangpatio_send_devres_queue == 0 )
    {
        while( 1 );
    }
}

/** ****************************************************************************
* @remarks       void national808_ctlock_queue_create( void )
* @brief         控锁消息队列创建
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void mangpatio_ctlock_queue_create( void )
{
	mangpatio_ctlock_queue = gaoz_queue_create( NET_ACK_QUEUE_MAX_NUM, sizeof( national808_ctrllock_msg_t ) );
    if( mangpatio_ctlock_queue == 0 )
    {
        while( 1 );
    }
}
/** ****************************************************************************
* @remarks       void national808_ctlock_res_queue_create( void )
* @brief         控锁异步消息队列创建
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void mangpatio_ctlock_res_queue_create( void )
{
	mangpatio_ctlock_res_queue = gaoz_queue_create( NET_ACK_QUEUE_MAX_NUM, sizeof( national808_ctrllock_msg_t ) );
    if( mangpatio_ctlock_res_queue == 0 )
    {
        while( 1 );
    }
}
/** ****************************************************************************
* @remarks       void national808_upgrade_queue_create( void )
* @brief         升級消息队列创建
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void mangpatio_upgrade_queue_create( void )
{
	mangpatio_upgrade_queue = gaoz_queue_create( NET_ACK_QUEUE_MAX_NUM, sizeof( update_result_t ) );
    if( mangpatio_upgrade_queue == 0 )
    {
        while( 1 );
    }
}
/** ****************************************************************************
* @remarks       static uint32_t mangpatio_net_patiores_queue_get( national_net_res_msg_t *res_msg , uint32_t delay_time )
* @brief         获取国标平台网络命令应答消息队列
* @param[in]     *res_msg 获取的数据地址
* @param[in]     delay_time 等待的时间
* @param[out]    无
* @return        pdPASS 接收数据成功 ，pdFAIL 接收数据失败
* @attention     无
*******************************************************************************/
static uint32_t mangpatio_net_patiores_queue_get( national_net_res_msg_t *res_msg , uint32_t delay_time )
{
    if( mangpatio_send_patiores_queue == 0 )
    {
        mangpatio_net_patiores_queue_create();
    }
    if(gaoz_queue_get( mangpatio_send_patiores_queue, res_msg, delay_time )  >= 0 ) //
    {
        return pdPASS;
    }
  
    return pdFAIL;
}

/** ****************************************************************************
* @remarks       static uint8_t mangpatio_net_patiores_queue_put( national_net_res_msg_t *res_msg )
* @brief         发送国标网络平台应答数据
* @param[in]     *res_msg 发送的数据地址
* @param[out]    无
* @return        1 发送数据成功 ，0 发送数据失败
* @attention     无
*******************************************************************************/
static uint8_t mangpatio_net_patiores_queue_put( national_net_res_msg_t *res_msg )
{
    if( gaoz_queue_put( mangpatio_send_patiores_queue, res_msg, 1000 ) == 0 )
    {
        return 1;
    }
	else
	{
		return 0;
	}
}
/** ****************************************************************************
* @remarks       static uint32_t mangpatio_net_devres_queue_get( national_net_res_msg_t *res_msg , uint32_t delay_time )
* @brief         获取国标平台网络数据应答消息队列
* @param[in]     *res_msg 获取的数据地址
* @param[in]     delay_time 等待的时间
* @param[out]    无
* @return        pdPASS 接收数据成功 ，pdFAIL 接收数据失败
* @attention     无
*******************************************************************************/
static uint32_t mangpatio_net_devres_queue_get( national808_net_devres_msg_t *res_msg , uint32_t delay_time )
{
    if( mangpatio_send_devres_queue == 0 )
    {
        mangpatio_net_devres_queue_create();
    }
    if(gaoz_queue_get( mangpatio_send_devres_queue, res_msg, delay_time )  == 0 ) //等待回应信号量
    {
        return pdPASS;
    }
    else
    {
        return pdFAIL;
    }
    return pdFAIL;
}

/** ****************************************************************************
* @remarks       static uint8_t mangpatio_net_devres_queue_put( national_net_res_msg_t *res_msg )
* @brief         发送国标网络数据应答数据
* @param[in]     *res_msg 发送的数据地址
* @param[out]    无
* @return        1 发送数据成功 ，0 发送数据失败
* @attention     无
*******************************************************************************/
uint8_t mangpatio_net_devres_queue_put( national808_net_devres_msg_t *res_msg )
{
    if( gaoz_queue_put( mangpatio_send_devres_queue, ( void * )res_msg, 1000 ) == 0 )
    {
        return 1;
    }
	else
	{
		return 0;
	}
}
/** ****************************************************************************
* @remarks       uint32_t national808_ctlock_queue_get( national808_ctrllock_msg_t *res_msg , uint32_t delay_time )
* @brief         获取控锁消息队列
* @param[in]     *res_msg 获取的数据地址
* @param[in]     delay_time 等待的时间
* @param[out]    无
* @return        pdPASS 接收数据成功 ，pdFAIL 接收数据失败
* @attention     无
*******************************************************************************/
uint32_t mangpatio_ctlock_queue_get( national808_ctrllock_msg_t *res_msg , uint32_t delay_time )
{
    if( mangpatio_ctlock_queue == 0 )
    {
        mangpatio_ctlock_queue_create();
    }
    if(gaoz_queue_get( mangpatio_ctlock_queue, res_msg, delay_time )  == 0 ) //等待回应信号量
    {
        return pdPASS;
    }
    else
    {
        return pdFAIL;
    }
    return pdFAIL;
}

/** ****************************************************************************
* @remarks       uint8_t mangpatio_ctlock_queue_put( national_net_res_msg_t *res_msg )
* @brief         发送控锁消息
* @param[in]     *res_msg 发送的数据地址
* @param[out]    无
* @return        1 发送数据成功 ，0 发送数据失败
* @attention     无
*******************************************************************************/
uint8_t mangpatio_ctlock_queue_put( national808_ctrllock_msg_t *res_msg )
{
    if( gaoz_queue_put( mangpatio_ctlock_queue, ( void * )res_msg, 1000 ) == 0 )
    {
        return 1;
    }
	else
	{
		return 0;
	}
}
/** ****************************************************************************
* @remarks       uint32_t national808_ctlock_res_queue_get( national808_ctrllock_msg_t *res_msg , uint32_t delay_time )
* @brief         获取控锁响应消息队列
* @param[in]     *res_msg 获取的数据地址
* @param[in]     delay_time 等待的时间
* @param[out]    无
* @return        pdPASS 接收数据成功 ，pdFAIL 接收数据失败
* @attention     无
*******************************************************************************/
static uint32_t mangpatio_ctlock_res_queue_get( national808_ctrllock_msg_t *res_msg , uint32_t delay_time )
{
    if( mangpatio_ctlock_res_queue == 0 )
    {
        mangpatio_ctlock_res_queue_create();
    }
    if(gaoz_queue_get( mangpatio_ctlock_res_queue, res_msg, delay_time )  == 0 ) //等待回应信号量
    {
        return pdPASS;
    }
    else
    {
        return pdFAIL;
    }
    return pdFAIL;
}

/** ****************************************************************************
* @remarks       uint8_t mangpatio_ctlock_res_queue_put( national_net_res_msg_t *res_msg )
* @brief         发送控锁响应消息
* @param[in]     *res_msg 发送的数据地址
* @param[out]    无
* @return        1 发送数据成功 ，0 发送数据失败
* @attention     无
*******************************************************************************/
uint8_t mangpatio_ctlock_res_queue_put( national808_ctrllock_msg_t *res_msg )
{
    if( gaoz_queue_put( mangpatio_ctlock_res_queue, ( void * )res_msg, 1000 ) == 0 )
    {
        return 1;
    }
	else
	{
		return 0;
	}
}

/** ****************************************************************************
* @remarks       uint32_t national808_upgrade_queue_get( update_result_t *res_msg , uint32_t delay_time )
* @brief         获取升级消息队列
* @param[in]     *res_msg 获取的数据地址
* @param[in]     delay_time 等待的时间
* @param[out]    无
* @return        pdPASS 接收数据成功 ，pdFAIL 接收数据失败
* @attention     无
*******************************************************************************/
static uint32_t mangpatio_upgrade_queue_get( update_result_t *res_msg , uint32_t delay_time )
{
    if( mangpatio_upgrade_queue == 0 )
    {
        mangpatio_ctlock_res_queue_create();
    }
    if(gaoz_queue_get( mangpatio_upgrade_queue, res_msg, delay_time )  == 0 ) //等待回应信号量
    {
        return pdPASS;
    }

    return pdFAIL;
}

/** ****************************************************************************
* @remarks       uint8_t mangpatio_upgrade_queue_put( national_net_res_msg_t *res_msg )
* @brief         发送升级消息
* @param[in]     *res_msg 发送的数据地址
* @param[out]    无
* @return        1 发送数据成功 ，0 发送数据失败
* @attention     无
*******************************************************************************/
uint8_t mangpatio_upgrade_queue_put( update_result_t *res_msg )
{
    if( gaoz_queue_put( mangpatio_upgrade_queue, ( void * )res_msg, 1000 ) == 0 )
    {
        return 1;
    }
	else
	{
		return 0;
	}
}

/** ****************************************************************************
* @remarks      static uint32_t mangpatio_get_updata_meter( uint32_t time_tick, mangpatio_info_t *curinf)
* @brief         距离统计,统计运行上报距离间隔
* @param[in]     
* @param[out]    无
* @return         pdFAIL 失败  pdPASS成功
* @attention     
*******************************************************************************/
static uint32_t mangpatio_get_updata_meter( uint32_t time_tick, mangpatio_info_t *curinf)
{
	uint32_t ret = pdFAIL;
	uint32_t currentspeed=0;

	n808_vehicle_data_t data_vehicle;
	vehicle_data_now_get(&data_vehicle);

	if(curinf == NULL)
	{
		return ret;
	}

	if( pdPASS == sys_timestamp_delay(( uint32_t * )&time_tick,  1*GAOZ_TICK_PERIOD_S) )
	{
		{
			currentspeed = data_vehicle.speed*100;
			mangpatio_min_mile += (mangpatio_allkilo_mod+currentspeed)/3600;
			mangpatio_allkilo_mod = (mangpatio_allkilo_mod+currentspeed)%3600;
			if(mangpatio_min_mile > hex4CombineOne(curinf->workReportMaxMeter))
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","<<->>min_mile = %d,workReportMaxMeter=%d\r\n",mangpatio_min_mile,hex4CombineOne(curinf->workReportMaxMeter));
				mangpatio_min_mile = 0;
				ret = pdPASS;
			}
		}
	}

	return ret;
}

/** ****************************************************************************
* @remarks       uint32_t national_data_manage_fun( national_send_data_e *send_st , national_response_e *recv_st)
* @brief         国标数据管理功能
* @param[in]     无
* @param[out]    *send_st 发送状态，×recv_st 接收状态
* @return        pdPASS 数据发送失败 pdFAIL 数据发送成功
* @attention     
*******************************************************************************/
static uint32_t mangpatio_data_manage_fun( national_send_data_e *send_st , national_response_e *recv_st)
{
    uint32_t ret = pdFAIL;
	uint8_t txbuf[1024]={ 0 };
    uint16_t txbuflen=0;
    national_run_state_e run_st;
	national808_net_devres_msg_t devres_msg;
	national808_ctrllock_msg_t ctlock_msg;
	update_result_t upgrade_msg;
	system_work_mode_s sys_st;

    sys_st = gaoz_sys_work_mode_get();
	run_st = mangpatio_run_state_get();
    *send_st = PLAT_SEND_INIT;
    *recv_st = PLAT_RESPONSE_INIT;
	/*检测网络运行状态，检测到处于数据状态时获取控制命令*/
	if( run_st == TER_RUNING_DATA )
	{
		if( vehicle_authentication_mangatio_st_get() == VEHICLE_AUTH_REQ )
		{
			run_st = TER_RUNING_AUTH;
			vehicle_authentication_mangatio_st_set( VEHICLE_AUTH_INIT );
		}
	}
	switch( run_st )
	{
		/*判断终端运行在初始化状态进行终端登陆平台*/
		case TER_RUNING_REG:
			if(terinf.AuthCodeFlag != 161)
			{
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = mangpatio_SendRegister(txbuf);
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{   
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_HAVE_RESPONSE;
						gaoz_log_debug_printf(INFOR_MODE,"800c","SendRegister:[0100] Send OK\r\n");	
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","SendRegister:[0100] Send ERROR\r\n");	
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
				}
				ret = pdPASS;    
            }
			else
			{
				mangpatio_run_state_set( TER_RUNING_AUTH );
			}
		break;
		case TER_RUNING_AUTH:
			mangpatio_set_time_tick = gaoz_get_systick(); /*获取当前最新的系统时钟*/
			mangpatio_herat_time_tick = gaoz_get_systick();
			mangpatio_set_meter_tick = gaoz_get_systick();
			if((terinf.AuthCodeFlag == 161))
            {
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = mangpatio_Authentication(txbuf);
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{   
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_HAVE_RESPONSE;
						gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio Authentication:[0501] Send OK\r\n");	
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","Authentication:[0501] Send ERROR\r\n");	
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);	
				}
				ret = pdPASS;
			}
			else
			{
				mangpatio_run_state_set( TER_RUNING_REG );
			}
		break;
		/*发送数据包*/
		case TER_RUNING_DATA:
		{
			if( sys_st == SYS_ST_RUN )
			{
				if( pdPASS == sys_timestamp_delay(( uint32_t * )&mangpatio_set_time_tick, hex4CombineOne(terinf.workReportMaxTime) * GAOZ_TICK_PERIOD_S ))
				{
					memset(txbuf,0,sizeof(txbuf));
					txbuflen = mangpatio_positioninfo(0,txbuf);
					//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
					if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
					{
						if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
						{   
							*send_st = PLAT_SEND_OK;
							*recv_st = PLAT_HAVE_RESPONSE;	
							gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_positioninfo:[0504] Send OK\r\n");	
						}
						else
						{
							*send_st = PLAT_SEND_ERR;
							gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_positioninfo:[0504] Send ERROR\r\n");		
						}
						mangpatio_herat_time_tick = gaoz_get_systick();	
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
					}
					ret = pdPASS;
					mangpatio_set_time_tick = gaoz_get_systick(); /*获取当前最新的系统时钟*/
					mangpatio_min_mile=0;
				}
				else
				{
					if( pdPASS ==  mangpatio_get_updata_meter(mangpatio_set_meter_tick,&terinf))
					{
						memset(txbuf,0,sizeof(txbuf));
						txbuflen = mangpatio_positioninfo(0,txbuf);
						//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
						if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
						{
							if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
							{   
								*send_st = PLAT_SEND_OK;
								*recv_st = PLAT_HAVE_RESPONSE;	
								gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_positioninfo:[0504] Send OK\r\n");	
							}
							else
							{
								*send_st = PLAT_SEND_ERR;
								gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_positioninfo:[0504] Send ERROR\r\n");	
							}
							mangpatio_herat_time_tick = gaoz_get_systick();	
						}
						else
						{
							*send_st = PLAT_SEND_ERR;
							gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
						}
						ret = pdPASS;
					}
					if( pdPASS == sys_timestamp_delay(( uint32_t * )&mangpatio_herat_time_tick, hex4CombineOne(terinf.heartBeatDly) * GAOZ_TICK_PERIOD_S ) )
					{
						memset(txbuf,0,sizeof(txbuf));
						txbuflen = mangpatio_SendHeart(txbuf);
						//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
						if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
						{
							if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
							{   
								*send_st = PLAT_SEND_OK;
								*recv_st = PLAT_HAVE_RESPONSE;	
								gaoz_log_debug_printf(INFOR_MODE,"800c","SendHeart:[0506] Send OK\r\n");	
							}
							else
							{
								*send_st = PLAT_SEND_ERR;
								gaoz_log_debug_printf(INFOR_MODE,"800c","SendHeart:[0506] Send ERROR\r\n");	
							}
						}
						else
						{
							*send_st = PLAT_SEND_ERR;
							gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
						}
						ret = pdPASS;
					}
					else
					{	
						/* 異步應答 */
						if( mangpatio_ctlock_res_queue_get( &ctlock_msg , 1000 ) == pdPASS )
						{	
							memset(txbuf,0,sizeof(txbuf));
							gaoz_log_debug_printf(INFOR_MODE,"800c","ctlock_msg.lock_err:%d,ctlock_msg.lock_res.respondsta:%d,ctlock_msg.lock_type:%d\r\n",ctlock_msg.lock_err,ctlock_msg.lock_res.respondsta,ctlock_msg.lock_type);
							txbuflen = mangpatio_lockrespond(txbuf,&ctlock_msg);
							//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
							if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
							{
								if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
								{   
									*send_st = PLAT_SEND_OK;
									*recv_st = PLAT_NO_RESPONSE;
									gaoz_log_debug_printf(INFOR_MODE,"800c","lockrespond Send OK:%x\r\n",ctlock_msg.lock_res.respondcmd);	
								}
								else
								{
									*send_st = PLAT_SEND_ERR;
									gaoz_log_debug_printf(INFOR_MODE,"800c","lockrespond Send ERROR\r\n");	
								}
								mangpatio_herat_time_tick = gaoz_get_systick();	
							}
							else
							{
								*send_st = PLAT_SEND_ERR;
								gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
							}
							ret = pdPASS;
							mangpatio_ctrllock_state_set(CTLOCK_OK);
						}
						
						/* upgrade */
						if( mangpatio_upgrade_queue_get( &upgrade_msg, 1000 ) == pdPASS)
						{
							memset(txbuf,0,sizeof(txbuf));
							txbuflen = mangpatio_updateresult(&upgrade_msg,txbuf);
							//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
							if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
							{
								if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
								{   
									*send_st = PLAT_SEND_OK;
									*recv_st = PLAT_NO_RESPONSE;
									gaoz_log_debug_printf(INFOR_MODE,"800c","upgrade:[0108] Send OK\r\n");		
								}
								else
								{
									*send_st = PLAT_SEND_ERR;
									gaoz_log_debug_printf(INFOR_MODE,"800c","upgrade:[0108] Send ERROR\r\n");	
								}
								mangpatio_herat_time_tick = gaoz_get_systick();	
							}
							else
							{
								*send_st = PLAT_SEND_ERR;
								gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
							}
							if(upgrade_msg.update_types != 0)
							{
								mangpatio_run_state_set(TER_RUNING_AUTH);
							}
							ret = pdPASS;
						}
					}
				}
			}
			else if(sys_st == SYS_ST_WAKE)
			{
				/* 異步應答 */
				if( mangpatio_ctlock_res_queue_get( &ctlock_msg , 1000 ) == pdPASS )
				{	
					memset(txbuf,0,sizeof(txbuf));
					gaoz_log_debug_printf(INFOR_MODE,"800c","ctlock_msg.lock_err:%d,ctlock_msg.lock_res.respondsta:%d,ctlock_msg.lock_type:%d\r\n",ctlock_msg.lock_err,ctlock_msg.lock_res.respondsta,ctlock_msg.lock_type);
					txbuflen = mangpatio_lockrespond(txbuf,&ctlock_msg);
					//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
					if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
					{
						if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
						{   
							*send_st = PLAT_SEND_OK;
							*recv_st = PLAT_NO_RESPONSE;	
							mangpatio_sleep_wakeup_state_set( SLEEP_WAKEUP_INIT );
							gaoz_log_debug_printf(INFOR_MODE,"800c","lockrespond:[0563] Send OK\r\n");	
						}
						else
						{
							*send_st = PLAT_SEND_ERR;
							gaoz_log_debug_printf(INFOR_MODE,"800c","lockrespond:[0563] Send ERROR\r\n");
						}
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
					}
					mangpatio_ctrllock_state_set( CTLOCK_OK);
					ret = pdPASS;
				}

				if( mangpatio_sleep_wakeup_state_get() != SLEEP_WAKEUP_INIT )
				{
					// gaoz_log_debug_printf(INFOR_MODE,"800E","800e mangpatio_sleep_wakeup_state_get:%d\r\n",mangpatio_sleep_wakeup_state_get());	
					gaoz_power_man_wakeup_task_num_run();
					 
					/* upgrade */
					if( mangpatio_upgrade_queue_get( &upgrade_msg, 1000 ) == pdPASS)
					{
						memset(txbuf,0,sizeof(txbuf));
						txbuflen = mangpatio_updateresult(&upgrade_msg,txbuf);
						//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
						if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
						{	
							if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
							{   
								*send_st = PLAT_SEND_OK;
								*recv_st = PLAT_NO_RESPONSE;
								mangpatio_sleep_wakeup_state_set( SLEEP_WAKEUP_INIT );
								gaoz_log_debug_printf(INFOR_MODE,"800c","upgrade:[0108] Send OK\r\n");	
								//gaoz_sys_switch_state_set(SYS_WRTC,SYS_WRTC_SLEEP);  /*set sleep*/
							}
							else
							{
								*send_st = PLAT_SEND_ERR;
								gaoz_log_debug_printf(INFOR_MODE,"800c","upgrade:[0108] Send ERROR\r\n");	
							}
						}
						else
						{
							*send_st = PLAT_SEND_ERR;
							gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
						}
						if(upgrade_msg.update_types != 0)
						{
							mangpatio_run_state_set(TER_RUNING_AUTH);
						}
						ret = pdPASS;
					}
					/*TIME休眠唤醒标志使能，发送数据包*/
					if( mangpatio_sleep_wakeup_state_get() == SLEEP_STATE_TIME )
					{
						memset(txbuf,0,sizeof(txbuf));
						txbuflen = mangpatio_positioninfo(0,txbuf);
						//ret = gaoz_socket_write( &socket_handle_2 , txbuf, txbuflen );
						if( gaoz_socket_write( &socket_handle_2 , txbuf, txbuflen ) == pdPASS )
						{
							if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
							{   
								*send_st = PLAT_SEND_OK;
								*recv_st = PLAT_HAVE_RESPONSE;	
								//mangpatio_data_st.send_st = N808_SEND_OK;
								mangpatio_sleep_wakeup_state_set( SLEEP_WAKEUP_INIT );
								gaoz_log_debug_printf(INFOR_MODE,"800c","sleep mangpatio_positioninfo:[0504] Send OK\r\n");	
							}
							else
							{
								*send_st = PLAT_SEND_ERR;
								gaoz_log_debug_printf(INFOR_MODE,"800c","sleep mangpatio_positioninfo:[0504] Send ERROR\r\n");	
							}
						}
						else
						{
							*send_st = PLAT_SEND_ERR;
							gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
						}
						ret = pdPASS;
					}
					/*RTC休眠唤醒标志位使能，发送数据包*/
					else if( mangpatio_sleep_wakeup_state_get() == SLEEP_STATE_RTC  ) 
					{
						memset(txbuf,0,sizeof(txbuf));
						txbuflen = mangpatio_SendHeart(txbuf);
						//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
						if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
						{
							if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
							{   
								*send_st = PLAT_SEND_OK;
								*recv_st = PLAT_HAVE_RESPONSE;	
								mangpatio_sleep_wakeup_state_set( SLEEP_WAKEUP_INIT );
								gaoz_log_debug_printf(INFOR_MODE,"800c","sleep SendHeart:[0506] Send OK\r\n");	
							}
							else
							{
								*send_st = PLAT_SEND_ERR;
								gaoz_log_debug_printf(INFOR_MODE,"800c","sleep SendHeart:[0506] Send ERROR\r\n");	
							}
							mangpatio_herat_time_tick = gaoz_get_systick();	
							mangpatio_set_time_tick = gaoz_get_systick();
							mangpatio_set_meter_tick = 	gaoz_get_systick();
						}
						else 
						{
							*send_st = PLAT_SEND_ERR;
							gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
						}
						ret = pdPASS;
					}	
					gaoz_power_man_wakeup_task_num_stop();				
				}
			}
		}
		break;
		/*终端设备登出*/
		case TER_RUNING_LOGOUT:	
			mangpatio_run_state_set( TER_RUNING_INIT );
			mangpatio_rst = NET_RST_END;
		break;
		/*平台查询系统版本号 成功*/
		case TER_RUNING_VERSION_TRUE:
			memset(txbuf,0,sizeof(txbuf));
			txbuflen = mangpatio_property(txbuf); 
			//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
			if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
			{
				if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
				{   
					*send_st = PLAT_SEND_OK;
					*recv_st = PLAT_NO_RESPONSE;	
					gaoz_log_debug_printf(INFOR_MODE,"800c","property:[0107] Send OK\r\n");	
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","property:[0107] Send OK\r\n");	
				}
			}
			else
			{
				*send_st = PLAT_SEND_ERR;
				gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
			}
			mangpatio_run_state_set( TER_RUNING_DATA );
			ret = pdPASS;
		break;
		/*平台查询版本号 失败*/
		case TER_RUNING_VERSION_FAIL:
			
		break;
		/*车辆运行OTA控制*/
		case TER_RUNING_CONTROL_FOTA_TRUE:
			if( mangpatio_net_devres_queue_get( &devres_msg , 1000 ) == pdPASS )
			{	
				gaoz_log_debug_printf(INFOR_MODE,"800c","devres_msg : %d,%d,%d\r\n",devres_msg.respondcmd,devres_msg.respondsn,devres_msg.respondsta);	
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = mangpatio_generalrespond(&devres_msg ,txbuf);
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{   
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_NO_RESPONSE;	
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9108] Send OK\r\n");	
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9108] Send ERROR\r\n");		
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
				}
				ret = pdPASS;
			}
			else
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_get( &devres_msg , 1000 ) fail\r\n");		
			}
			mangpatio_run_state_set( TER_RUNING_DATA );			
		break;
		case TER_RUNING_CONTROL_FOTA_FAIL:
			
		break;
		/*车辆运行双闪控制*/
		case TER_RUNING_CONTROL_FLASHING_TRUE:
			if( mangpatio_net_devres_queue_get( &devres_msg , 1000 ) == pdPASS )
			{	
				gaoz_log_debug_printf(INFOR_MODE,"800c","devres_msg : %d,%d,%d\r\n",devres_msg.respondcmd,devres_msg.respondsn,devres_msg.respondsta);	
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = mangpatio_generalrespond(&devres_msg ,txbuf);
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{   
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_NO_RESPONSE;
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9562] Send OK\r\n");	
						if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)	
						{
							vehiclect_runing_state_set(CARCTL_RUNING_SPEAK_LIGHT);/*鸣笛闪灯状态*/
						}		
						else
						{
							mangpatio_ctrllock_state_set(CTLOCK_OK);
						}
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9562] Send ERROR\r\n");	
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
				}
				ret = pdPASS;
			}
			else
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_get( &devres_msg , 1000 ) fail\r\n");		
			}
			mangpatio_ctrllock_state_set( CTLOCK_OK);
			mangpatio_run_state_set( TER_RUNING_DATA );
		break;
		case TER_RUNING_CONTROL_FLASHING_FAIL:
			
		break;
		/*车辆运行定位车辆控制*/
		case TER_RUNING_CONTROL_LOCATE_TRUE:
			
		break;
		case TER_RUNING_CONTROL_LOCATE_FAIL:
			
		break;
		/*车辆运行空调控制*/
		case TER_RUNING_CONTROL_AIR_COND_TRUE:

		break;
		case TER_RUNING_CONTROL_AIR_COND_FAIL:
		
		break;
		/*车辆运行车锁控制*/
		case TER_RUNING_CONTROL_DOOR_TRUE:
			if( mangpatio_net_devres_queue_get( &devres_msg , 1000 ) == pdPASS )
			{	
				gaoz_log_debug_printf(INFOR_MODE,"800c","devres_msg : %d,%d,%d\r\n",devres_msg.respondcmd,devres_msg.respondsn,devres_msg.respondsta);	
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = mangpatio_generalrespond(&devres_msg ,txbuf);
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{   
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_NO_RESPONSE;	
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9563] Send OK,Control Door True\r\n");	
						if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
						{
							vehiclect_runing_state_set(CARCTL_RUNING_LOCK_UNLOCK);/*控锁状态*/	
						}
						else
						{
							mangpatio_ctrllock_state_set( CTLOCK_OK);
						}
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9563] Send ERROR,Control Door True\r\n");	
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
				}
				ret = pdPASS;
			}
			else
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_get( &devres_msg , 1000 ) fail\r\n");		
			}
			mangpatio_ctrllock_state_set( CTLOCK_OK);
			mangpatio_run_state_set( TER_RUNING_DATA );
		break;
		case TER_RUNING_CONTROL_DOOR_FAIL:
			if( mangpatio_net_devres_queue_get( &devres_msg , 1000 ) == pdPASS )
			{	
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = mangpatio_generalrespond(&devres_msg ,txbuf);
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{   
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_NO_RESPONSE;	
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9563] Send OK,Control Door Fail\r\n");	
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9563] Send ERROR,Control Door Fail\r\n");	
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
				}
				ret = pdPASS;
			}	
			mangpatio_run_state_set( TER_RUNING_DATA );
			//mangpatio_set_time_tick = gaoz_get_systick(); /*获取当前最新的系统时钟*/
			mangpatio_herat_time_tick = gaoz_get_systick();
		break;
		/*车辆运行发动机控制*/
		case TER_RUNING_CONTROL_ENGINE_TRUE:
		
		break;
		case TER_RUNING_CONTROL_ENGINE_FAIL:
			
		break;
		/*车辆运行车窗控制*/
		case TER_RUNING_CONTROL_WIND_TRUE:
			
		break;
		case TER_RUNING_CONTROL_WIND_FAIL:
			
		break;
		/*车辆运行天窗控制*/
		case TER_RUNING_CONTROL_ROOF_TRUE:
			
		break;
		case TER_RUNING_CONTROL_ROOF_FAIL:
			
		break;
		/*OTA远程升级过程*/
		case TER_RUNING_FOTA_SATRT:
			
		break;
		/*OTA升级中*/
		case TER_RUNING_FOTA_UPGRADE:
			
		break;
		/*通知平台刷新固件*/
		case TER_RUNING_FIRMWARE_SATRT:
			
		break;
		/*本地刷新固件成功*/
		case TER_RUNING_FIRMWARE_SUCCESS:
			
		break;
		/*本地刷新固件失败*/
		case TER_RUNING_FIRMWARE_FAIL:
			
		break;
		/*平台查询系统参数成功*/
		case TER_RUNING_QUERY_SYS_PAR_TRUE:
			if( mangpatio_net_devres_queue_get( &devres_msg , 1000 ) == pdPASS )
			{
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = mangpatio_parameter(&devres_msg ,txbuf); 
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{   
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_NO_RESPONSE;
						gaoz_log_debug_printf(INFOR_MODE,"800c","property:[0590] Send OK\r\n");		
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","property:[0590] Send ERROR\r\n");	
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
				}
				ret = pdPASS;
			}
			mangpatio_run_state_set( TER_RUNING_DATA );
		break;
		/*平台参数系统参数失败*/
		case TER_RUNING_QUERY_SYS_PAR_FAIL:
			
		break;	
		/*平台设置系统参数成功*/
		case TER_RUNING_SET_SYS_PAR_TRUE:
			if( mangpatio_net_devres_queue_get( &devres_msg , 1000 ) == pdPASS )
			{	
				gaoz_log_debug_printf(INFOR_MODE,"800c","devres_msg : %d,%d,%d\r\n",devres_msg.respondcmd,devres_msg.respondsn,devres_msg.respondsta);	
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = mangpatio_generalrespond(&devres_msg ,txbuf);
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{   
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_NO_RESPONSE;	
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9529] Send OK\r\n");	
						if(mangpatio_rst == NET_RST_START)
						{
							mangpatio_run_state_set( TER_RUNING_LOGOUT );
						}
						else
						{
							mangpatio_run_state_set( TER_RUNING_DATA );
						}
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9529] Send ERROR\r\n");	
						mangpatio_run_state_set( TER_RUNING_DATA );
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
				}
				ret = pdPASS;
			}
			else
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_get( &devres_msg , 1000 ) fail\r\n");		
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
		break;	
		/*平台设置系统参数失败*/
		case TER_RUNING_SET_SYS_PAR_FAIL:
			if( mangpatio_net_devres_queue_get( &devres_msg , 1000 ) == pdPASS )
			{	
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = mangpatio_generalrespond(&devres_msg ,txbuf);
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{   
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_NO_RESPONSE;	
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9529] Send OK\r\n");
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9529] Send ERROR\r\n");
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
				}
				ret = pdPASS;
			}
			mangpatio_run_state_set( TER_RUNING_DATA );
		break;	
		case TER_RUNING_QUERY_DATA:
			memset(txbuf,0,sizeof(txbuf));
			txbuflen = mangpatio_positioninfo(1,txbuf); 
			//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
			if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
			{
				if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
				{   
					*send_st = PLAT_SEND_OK;
					*recv_st = PLAT_NO_RESPONSE;	
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_positioninfo:[0560] Send OK\r\n");	
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_positioninfo:[0560] Send ERROR\r\n");		
				}
			}
			else
			{
				*send_st = PLAT_SEND_ERR;
				gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
			}
			ret = pdPASS;
			mangpatio_run_state_set( TER_RUNING_DATA );
		break;
		case TER_RUNING_CONTROL_BT:
			if( mangpatio_net_devres_queue_get( &devres_msg , 1000 ) == pdPASS )
			{	
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = mangpatio_generalrespond(&devres_msg ,txbuf);
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{   
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_NO_RESPONSE;	
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9579] Send OK\r\n");	
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[9579] Send ERRORK\r\n");	
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
				}
				ret = pdPASS;
			}
			mangpatio_run_state_set( TER_RUNING_DATA );
		break;
		case TER_RUNING_GENERAL_RSP:
			if( mangpatio_net_devres_queue_get( &devres_msg , 1000 ) == pdPASS )
			{	
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = mangpatio_generalrespond(&devres_msg ,txbuf);
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_NO_RESPONSE;	
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[0520] Send OK\r\n");	
						if((devres_msg.respondcmd == 0x9565) && (devres_msg.respondsta == 0))
						{
							if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
							{
								vehiclect_runing_state_set(CARCTL_RUNING_RELAY);/*断电状态*/
							}
						}
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","generalrespond:[0520] Send ERROR\r\n");	
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
				}
				if(devres_msg.respondcmd == 0x9571)
				{
					if( *send_st == PLAT_SEND_OK )
					{
						mangpatio_run_state_set( TER_RUNING_AUTH );
						memcpy(currinf.ManageID,terinf.ManageID,6);
						//mangpatio_data_st.send_st = N808_SENDRSP_OK;//切换以后重新读发送数据
					}
				}
				else
				{
					mangpatio_run_state_set( TER_RUNING_DATA );	
				}
				ret = pdPASS;
			}
			if( *send_st != PLAT_SEND_OK )
			{
				mangpatio_run_state_set( TER_RUNING_DATA );
				memcpy(currinf.ManageID,terinf.ManageID,6);
			}	
		break;
		case TER_RUNING_QUERY_RSA:
			if( mangpatio_net_devres_queue_get( &devres_msg , 1000 ) == pdPASS )
			{	
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = mangpatio_rsarespond(&devres_msg ,txbuf);
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{   
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_NO_RESPONSE;
						gaoz_log_debug_printf(INFOR_MODE,"800c","rsarespond:[0581] Send OK\r\n");		
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","rsarespond:[0581] Send ERROR\r\n");		
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
				}
				ret = pdPASS;
			}	
			mangpatio_run_state_set( TER_RUNING_DATA );
		break;
		case TER_RUNING_QUERY_QXWZ:
			if( mangpatio_net_devres_queue_get( &devres_msg , 1000 ) == pdPASS )
			{	
				memset(txbuf,0,sizeof(txbuf));
				txbuflen = qxwzrespond(&devres_msg ,txbuf);
				//ret = gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen );
				if( gaoz_socket_write( &socket_handle_2 , txbuf , txbuflen ) == pdPASS )
				{
					if( mangpatio_socket_send_data_suc( &socket_state_2 ,1000 ) == pdPASS ) /*检测底层回调函数的结果，是否已经发生成功*/
					{   
						*send_st = PLAT_SEND_OK;
						*recv_st = PLAT_NO_RESPONSE;	
						gaoz_log_debug_printf(INFOR_MODE,"800c","rsarespond:[0636] Send OK\r\n");	
					}
					else
					{
						*send_st = PLAT_SEND_ERR;
						gaoz_log_debug_printf(INFOR_MODE,"800c","rsarespond:[0636] Send ERROR\r\n");	
					}
				}
				else
				{
					*send_st = PLAT_SEND_ERR;
					gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]gaoz_socket_write fail\r\n", __FUNCTION__, __LINE__);
				}
				ret = pdPASS;
			}
			mangpatio_run_state_set( TER_RUNING_DATA );
		break;
        default:
            break;
	}

	if(ret == pdPASS)
	{
		gaoz_power_man_wakeup_task_num_run(); 
	}

	return ret;
}
/** ****************************************************************************
* @remarks       static uint32_t national_wait_response_result( uint32_t wait_time )
* @brief         国标数据应答状态分析
* @param[in]     wait_time 延时的时间设置 单位ms
* @param[out]    无
* @return        pdPASS 成功 pdFAIL 失败
* @attention     无
*******************************************************************************/
static uint32_t mangpatio_wait_response_result( uint32_t wait_time )
{
	uint32_t ret = pdFAIL;
	national_net_res_msg_t net_res_msg;
	
	if( mangpatio_net_patiores_queue_get( &net_res_msg , wait_time ) == pdPASS )
	{	/* 解析数据应答的结果 */
		gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_get ok%d,%d\r\n",net_res_msg.commane,net_res_msg.results);	
		mangpatio_data_analytic_fun( &net_res_msg );
		if(net_res_msg.commane == TER_REALTIME_DATA_REQ)
		{
			ret = pdPASS;
		}
		else
		{
			ret = net_res_msg.results;
		}
	}
	else
	{
		gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_get fail\r\n");	
		ret = pdFAIL;
	}
	return ret;
}

/** ****************************************************************************
* @remarks       static uint32_t national_plat_time_delay( uint32_t wait_time )
* @brief         等待平台消息延时功能
* @param[in]     wait_time 延时的时间设置 单位ms
* @param[out]    无
* @return        pdPASS 成功 pdFAIL 失败
* @attention     无
*******************************************************************************/
static uint32_t mangpatio_plat_time_delay( uint32_t wait_time )
{
	uint32_t ret = pdFAIL;
	national_net_res_msg_t net_res_msg;
	if ( mangpatio_run_state_get() == TER_RUNING_DATA )
	{
		if( mangpatio_net_patiores_queue_get( &net_res_msg , wait_time ) == pdPASS )
        {   /* 解析数据应答的结果 */
            mangpatio_data_analytic_fun( &net_res_msg );
            ret = net_res_msg.results;
			gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_data_analytic_fun commane: %d,result:%d\r\n",net_res_msg.commane,net_res_msg.results);
        }

	}
	else
	{	
		gaoz_task_sleep( wait_time );
	}
	return ret;
}

/** ****************************************************************************
* @remarks       void national_data_analytic_fun( national_response_e *nat_res)
* @brief         国标数据应答状态分析
* @param[in]     无
* @param[out]    *net_res 网络平台应答结果
* @return        无
* @attention     在系统启动后进行创建
*******************************************************************************/
static void mangpatio_data_analytic_fun( national_net_res_msg_t *net_res)
{
	switch( net_res->commane )
	{	
		/*注册平台*/
		case TER_RESPONSE_REG:		/*注册 平台应答*/
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_AUTH );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_REG );
			}
		break;
		/*鉴权平台*/
		case TER_RESPONSE_AUTH:		/*鉴权 平台应答*/
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_AUTH );
			}
			vehicle_authentication_mangatio_st_set( VEHICLE_AUTH_INIT );
		break;		
		/*终端心跳应答*/
		case TER_RESPONSE_HEART: 	/*终端心跳平台应答*/
			if( net_res->results == pdPASS )
			{	
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
		break;
		/*终端数据实时数据*/
		case TER_REALTIME_DATA_REQ:	/*实时数据平台应答*/
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_DATA );
				//mangpatio_data_st.send_st = N808_SENDRSP_OK;
			}
			else
			{
				mangpatio_run_state_set( TER_RUNING_DATA );
				//mangpatio_data_st.send_st = N808_SEND_WAIT;
			}
		break;
		/*终端数据补发数据*/
		case TER_RESEND_DATA_REQ: /*补发数据平台应答*/
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
		break;
		/*终端登出应答*/
		case TER_RESPONSE_LOGOUT:
		break;
		/*终端下发查询系统版本号*/
		case TER_QUERY_SYS_VERSION:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_VERSION_TRUE );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
		break;
		/*平台下发控制 -- OTA升级*/
		case TER_CONTROL_TER_FOTA:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_CONTROL_FOTA_TRUE );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
		break;
		/*平台下发控制 -- 双闪鸣笛*/
		case TER_CONTROL_TER_FLASHING_HORNS:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_CONTROL_FLASHING_TRUE );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
		break;
		/*平台下发控制 -- 远程定位车辆*/
		case TER_CONTROL_TER_LOCATE_VEHICLE:
		break;
		/*平台下发控制 -- 远程开始空调*/
		case TER_CONTROL_TER_START_AIR_COND:
		break;
		/*平台下发控制 -- 车门解锁*/
		case TER_CONTROL_TER_DOOR_UNCLOCK:
		break;
		/*平台下发控制 -- 远程启动/熄火发动机*/
		case TER_CONTROL_TER_START_ENGINE:
		break;
		/*平台下发控制 -- 远程升降车窗*/
		case TER_CONTROL_TER_UP_DOWN_WIND:
		break;
		/*平台下发控制 -- 远程天窗控制*/
		case TER_CONTROL_TER_ROOF_OPENING:
		break;
		case TER_CONTROL_TER_BT_UPDATESE:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_CONTROL_BT );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_CONTROL_BT );
			}
		break;
		case TER_CONTROL_TER_QXWZKEY_SET:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_GENERAL_RSP );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_GENERAL_RSP );
			}
		break;
		case TER_CONTROL_TER_QXWZKEY_CLE:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_GENERAL_RSP );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_GENERAL_RSP );
			}
		break;
		case TER_CONTROL_TER_UPDATE_ID:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_GENERAL_RSP );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_GENERAL_RSP );
			}
		break;
		case TER_CONTROL_TER_POWER:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_GENERAL_RSP );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_GENERAL_RSP );
			}
		break;
		/***********************************************************************************************/
		/*FOTA开始下载固件*/
		case TER_FOTA_DOWNLOAD_START:
		break;	
		case TER_FOTA_DOWNLOAD_STOP:
		break;	
		/***********************************************************************************************/
		/*固件下载成功*/
		case TER_FOTA_DOWNLOAD_SUCCESS:
		break;		
		/*固件升级失败*/
		case TER_FOTA_DOWNLOAD_ERR:
		break;
		/***********************************************************************************************/
		/*开始升级固件 成功*/
		case TER_FOTA_FIRMWARE_START:
		break;
		/*开始升级固件 失败*/
		case TER_FOTA_FIRMWARE_STOP:
		break;
		/***********************************************************************************************/
		/*应答本地升级固件 成功*/
		case TER_FOTA_FIRMWARE_SUCCESS:
		break;
		/*应答本地升级固件 失败*/
		case TER_FOTA_FIRMWARE_ERR:
		break;
		/*应答本地升级固件 超时*/	
		case TER_FOTA_FIRMWARE_TIME_OUT:
		break;
		case TER_QUERY_SYS_PARRMETER:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_QUERY_SYS_PAR_TRUE );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
		break;
		case TER_SET_SYS_PARRMETER:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_SET_SYS_PAR_TRUE );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_SET_SYS_PAR_FAIL );
			}
		break;
		case TER_QUERY_REALTIME:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_QUERY_DATA );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
		break;
		case TER_QUERY_RSA:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_QUERY_RSA );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
		break;
		case TER_QUERY_QXWZPARA:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_QUERY_QXWZ );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_DATA );
			}
		break;
		case TER_CONTROL_TERMINAL_ACK:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_CONTROL_DOOR_TRUE );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_CONTROL_DOOR_FAIL );
			}
		break;
		case TER_CONTROL_TER_RESET:
			if( net_res->results == pdPASS )
			{
				mangpatio_run_state_set( TER_RUNING_GENERAL_RSP );
			}
			else
			{	
				mangpatio_run_state_set( TER_RUNING_GENERAL_RSP );
			}
		break;
        default:
            break;
	}
}

/** ****************************************************************************
* @remarks       static void mangpatio_net_control_state_check( void )
* @brief         网络平台控制命令状态检测
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void mangpatio_net_control_state_check( void )
{
	#define MANGPATIO_CHECK_TIME_TICK ( 10 )				/*检测TICK时间10S*/
	static uint32_t mangpatio_check_time_tick = 0;
	
	if( (mangpatio_ctrllock_state_get() != CTLOCK_INIT) && (mangpatio_ctrllock_state_get() != CTLOCK_OK))
	{	
		gaoz_log_debug_printf(DEBUG_MODE,"800b","mangpatio_ctrllock_state_get() == %d\n", mangpatio_ctrllock_state_get() );
		gaoz_power_man_wakeup_task_num_run();				/*防止系统进入休眠状态*/ 
		if( pdPASS == sys_timestamp_delay(( uint32_t * )&mangpatio_check_time_tick, MANGPATIO_CHECK_TIME_TICK * GAOZ_TICK_PERIOD_S ) )
		{
			mangpatio_ctrllock_state_set( CTLOCK_OK );
			gaoz_log_debug_printf(DEBUG_MODE,"800b","mangpatio_ctrllock_state_set( CTLOCK_OK )\n");
		}
		gaoz_power_man_wakeup_task_num_stop();
	}
	else
	{
		mangpatio_check_time_tick = gaoz_get_systick();		/*获取系统tick时钟*/
	}
}


/** ****************************************************************************
* @remarks       void  mangpatio_net_st_check_task( void * param)
* @brief         网络状态监控
* @param[in]     * param 任务参数
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void mangpatio_net_st_check_task(void *param)
{
	uint8_t authInterValTime[] = {1,3,5,10};
	uint8_t authTime = 0;
	uint8_t i = 0;
	int ret = 0;
    INT8 host[64] = {0};		//"47.94.40.171";//"47.93.235.246";//"iothub.xiaojukeji.com";
	uint32_t port1=0;
	uint8_t terinfo[256] = {0};
	uint16_t terinfolen = 0;
	static uint8_t mploopcnt = 0;
	gaoz_task_sleep( 3000 ); 	//延迟3S钟等待系统参数检测完毕
	gaoz_mon_reg_task_dog( 60 * GAOZ_TICK_PERIOD_S ,(uint8_t *)MANG_SENDTASK_NAME ,TASK_SEM_YES);
	for( ;; )
	{
		gaoz_mon_reset_task_dog();
		ret = file_termpara_read(TERMID_FILE,(INT8 *)terid);
		if(ret < 0)
		{
			gaoz_task_sleep(1000); 
			continue;
		}
		else
		{
			gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]terid [%02x,%02x,%02x,%02x]", __FUNCTION__, __LINE__,terid[0],terid[1],terid[2],terid[3]);			
			gaoz_task_sleep(1000); 
			ret = -1;
			ret = mangpatio_readTerminalPara(MANGPATIOR_FILE);
			if(ret < 0)
			{
				//增加800头
				uint8_t tp[10]={0};
				tp[0] = 0x80;
				tp[1] = 0x0f&((terid[0]>>4)&0x0f);
				tp[2] = (((terid[0]&0x0f)<<4)&0xf0)|((terid[1]>>4)&0x0f);
				tp[3] = (((terid[1]&0x0f)<<4)&0xf0)|((terid[2]>>4)&0x0f);
				tp[4] = (((terid[2]&0x0f)<<4)&0xf0)|((terid[3]>>4)&0x0f);
				tp[5] = (((terid[3]&0x0f)<<4)&0xf0)&0xf0;
				memcpy(terinfo,terid,4);
				memcpy(&terinfo[4],"EDTT",4);
				mangpatio_initTerminalParaAll(tp,&terinfo[8],&terinfolen);//default;
				ret = file_termpara_write(MANGPATIOR_FILE,(INT8 *)terinfo,terinfolen+8);
				if(ret > 8)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","MANGPATIOR_FILE( OK )[%d]",ret);
				}
				log_debug_printf(ERROR_MODE,MANG_SENDTASK_NAME ,"[%s]get minor terminal para fail used default\r\n",sys_err_infor_pointer_get( 51 ));
			}
			else
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]mangpatio Parm [%d,%d,%d,%d]", __FUNCTION__, __LINE__,hex4CombineOne(terinf.workReportMaxTime),hex4CombineOne(terinf.idleReportMaxTime),hex4CombineOne(terinf.workReportMaxMeter),hex4CombineOne(terinf.idleReportMaxMeter));
				gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]ManageID [%02x,%02x,%02x,%02x,%02x,%02x]", __FUNCTION__, __LINE__,terinf.ManageID[0],terinf.ManageID[1],terinf.ManageID[2],terinf.ManageID[3],terinf.ManageID[4],terinf.ManageID[5]);
				gaoz_log_debug_printf(INFOR_MODE,"800c","[%s-%d]Auth success![%d,%d]", __FUNCTION__, __LINE__,hex4CombineOne(terinf.AuthCode),(terinf.AuthCodeFlag));
			}
			/*判断主网车辆ID是否有效，若有效统一用主网的车辆id*/
			if(strlen((char *)taginf.ManageID) != 0)
			{
				memcpy(terinf.ManageID,taginf.ManageID,sizeof(taginf.ManageID));
			}
		}	
		port1 = 0;
		memset(host,0,sizeof(host));
		DecASCII2HexDWORD((uint8_t *)terinf.Port1,terinf.Port1Len,&port1);
		memcpy(host,terinf.IP1,terinf.IP1Len);
		if((host == 0) || (port1 == 0)) 
		{
			gaoz_task_sleep(5000); 
			continue;
		}

		/*调试用***********************************************************************/
		// memset((uint8_t *)host, 0x00, sizeof( host ));
		// memcpy((char *)host, "47.93.235.246",strlen( (const char *)"47.93.235.246"));
		// port1 = 50000;
		
		gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio host[%s]\n", host);
		gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio port[%d]\n", port1);
		/**检测辅助平台IP地址是否有效，等待主平台释放网络资源**/
		for( ;; )
		{
			gaoz_mon_reset_task_dog();
			if((mangpatio_net_switch_st_check(&terinf) == pdPASS) && (get_twonetipdomain_equal(&taginf,&terinf) == pdPASS))
			{
				gaoz_log_debug_printf( INFOR_MODE,"800c","Secondary platform IP address is valid,Start up[%s:%d]\n", host, port1 );
				break;
			}
			else
			{
				gaoz_task_sleep(5000); 
			}
		}
	#if 1/*2022.05.09 YXX 不需要等待，直接注册网络即可。底层网络没有成功时直接返回错误标志*/
		while(mploopcnt < 10)
		{
			gaoz_power_man_wakeup_task_num_run(); 
			gaoz_task_sleep(1000); 
			mploopcnt++;
			gaoz_power_man_wakeup_task_num_stop();
		}
		mploopcnt = 0;
	#endif

		for(;;)
		{
			gaoz_mon_reset_task_dog();
			if( mangpatio_net_create_st_check( &socket_handle_2 , &socket_state_2 , host, port1 ) != pdPASS )
			{
				if(mangpatio_auth_recv_st >=5)
				{
					mangpatio_run_state_set( TER_RUNING_INIT );
					mangpatio_auth_fail_times++;
					gaoz_log_debug_printf( INFOR_MODE,"800c","mangpatio auth fail time mangpatio_auth_recv_st:%d mangpatio_auth_fail_times=%d \n", mangpatio_auth_recv_st,mangpatio_auth_fail_times);
					authTime = mangpatio_auth_fail_times; 	
					authTime = authTime > 3 ? 3 : authTime;
					authTime = authInterValTime[authTime];
					gaoz_log_debug_printf( INFOR_MODE,"800c"," --mangpatio auth fail wait time :%d min \n", authTime); 
					for(i=0;i<authTime;i++)//鉴权间隔时间 1，1,3,5,10 
					{ 
						gaoz_task_sleep(30000);  
						gaoz_mon_reset_task_dog();	 
						gaoz_task_sleep(30000);  
						gaoz_mon_reset_task_dog();	 
						gaoz_log_debug_printf( INFOR_MODE,"800c","mangpatio auth fail wait time :%d min --\n", i);  
					}

					mangpatio_auth_recv_st = 0;

				}	
				gaoz_task_sleep(1000); /*等待网络稳定，重新进行网络连接*/
			}
			else
			{
				if(mangpatio_rst == NET_RST_END)
				{
					mangpatio_rst = NET_RST_INT;
					mangpatio_saveserviceloadpara(NULL,2,NULL);//clear authcode
					socket_close(&socket_handle_2);
					socket_state_2.create_socket_state = SOCKET_LINK_CREATE_ERROR;
					break;
				}
				gaoz_task_sleep(100);  
			}  
		}                  
	}
}

/** ****************************************************************************
* @remarks       void  mangpatio_send_data_handle_task( void * param)
* @brief         处理808发送数据处理任务
* @param[in]     * param 任务参数
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void mangpatio_send_data_handle_task(void *param)
{
    ( void ) param;

	gaoz_mon_reg_task_dog( 60 * GAOZ_TICK_PERIOD_S ,(uint8_t *)MANG_SENDTASK_NAME ,TASK_SEM_YES);
	for( ;; )
    {
        gaoz_mon_reset_task_dog();
		/*为了解决系统休眠以后不能快速进入主程序进行数据处理，将网络休眠状态给屏蔽掉。YXX，2022.07.25*/
		if( (socket_state_2.create_socket_state != SOCKET_LINK_CREATE_OK ) )// || (mangpatio_netready != NET_READY_OK))
		{
			gaoz_task_sleep( 100 );
			continue;
		}
        if( mangpatio_data_manage_fun(&mangpatio_send_data , &mangpatio_response ) == pdPASS )
        {
            if( mangpatio_send_data == PLAT_SEND_OK )          /*发送数据成功*/
            {
                mangpatio_net_data.send_st = 0;
                if( mangpatio_response == PLAT_HAVE_RESPONSE ) /*等待平台应答*/
                {
                    if( mangpatio_wait_response_result( NET_ACK_QUEUE_RX_DATA_TIME ) == pdPASS )
                    {   /* 网络数据接收成功 */
                        mangpatio_net_data.recv_st = 0;
                    }
                    else
                    {   /* 网络数据接收失败 */ 
						if( mangpatio_runing_st == TER_RUNING_AUTH )
						{
							mangpatio_auth_recv_st++;
						}

                       mangpatio_net_data.recv_st ++;         /*接收数据错误状态计数器*/
					   /*if ( mangpatio_run_state_get() == TER_RUNING_DATA )
					   {
						   mangpatio_data_st.send_st = N808_SEND_WAIT;
					   }*/
					   gaoz_task_sleep(1000);					/*数据接收失败后等等1S中防止异常出现数据快速发送*/
                    }
                }
				gaoz_power_man_wakeup_task_num_stop();
            }
            else
            {
                mangpatio_net_data.send_st ++;                /*发送数据错误状态计数器，延时1S中重新进行发送*/
				gaoz_task_sleep(1000);
            }
        }
		else
		{
			mangpatio_plat_time_delay( 100 );               /*使用消息邮箱检测平台主动下发的数据，增强信息处理的及时性*/
		}
    }
}
/** ****************************************************************************
* @remarks       static void  national_recv_data_handle_task( void * pvParameters)
* @brief         处理808接收数据任务
* @param[in]     * pvParameters 任务参数
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void  mangpatio_recv_data_handle_task( void * pvParameters)
{ 
    ( void ) pvParameters;
	uint8_t *read_buf = NULL;
    uint16_t read_len = 0;
    socket_handle_s *handle = NULL;

	handle = &socket_handle_2;
    read_buf = (uint8_t *)gaoz_fibo_malloc( NET_RECV_DATA_LEN + 8 );
    gaoz_mon_reg_task_dog( 60 * GAOZ_TICK_PERIOD_S ,(uint8_t * )MANG_RECVTASK_NAME ,TASK_SEM_YES);
    for( ;; )
    {
        gaoz_mon_reset_task_dog();
        read_len = socket_recv( (socket_handle_s  *)handle, read_buf ,1000 );
        if( read_len > 0 ) 
        {
			gaoz_power_man_wakeup_task_num_run(); 
			parse_mangpatio_recv_data(read_buf, read_len);
			gaoz_power_man_wakeup_task_num_stop();
        }
		mangpatio_net_control_state_check();
    }
}
/** ****************************************************************************
* @remarks      uint16_t SendRegister(uint8_t *da)
* @brief         注册
* @param[in]     无
* @param[out]    *da缓存区
* @return        缓存区长度
* @attention     
*******************************************************************************/
static uint16_t mangpatio_SendRegister(uint8_t *da)
{
	uint8_t idx = 0;
	uint8_t term_id[4] = { 0 };
	uint8_t AuthDa[100] = { 0 };	
	uint16_t txLen = 0;
	uint8_t ccid[20]={0};

	//Message_ID
	idx = 0;
	memcpy(&AuthDa[idx], "\x01\x00",2);
	idx += 2;

	//Message_Property,afterward
	//memcpy(&AuthDa[idx], "\x00\x05",2);
	idx += 2;

	//Terminal_Num
	memcpy(&AuthDa[idx],terinf.ManageID, 6);
	idx += 6;

	//Message_Serial_Number
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum >> 8);
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum);
	mangpatioserialnum ++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}

	//省域ID

	//memcpy(&AuthDa[idx],Current_Info.ProvinceID, 2);
	memcpy(&AuthDa[idx],"61", 2);	//陕西
	//memset(&AuthDa[idx], 0, 2);
	idx += 2;
	//市县域ID
	//memcpy(&AuthDa[idx],Current_Info.TownID, 2);
	memset(&AuthDa[idx], '0', 2);
	idx += 2;

	//制造商ID
	memcpy(&AuthDa[idx], "GZCo.", 5);
	idx += 5;
	
	//终端型号
	memset((uint8_t *)&AuthDa[idx], 0x00, 20 );
	memcpy((uint8_t *)&AuthDa[idx], "HRC-001A", 8 );
	//memcpy(&AuthDa[idx], "car terminalRentE150", 20);	
	idx += 20;

	//终端ID
	memcpy(term_id,terid /*"\x01\x00\x07\x28"*/, 4);
	memcpy(&AuthDa[idx], term_id, 4);
	idx += 4;
	memset(&AuthDa[idx], 0, 3);
	idx += 3;

	//车牌颜色
	AuthDa[idx++] = 0;

	//车辆标识
	//memset(&AuthDa[idx], 0x38, 17);
	//idx += 17;
	//20160428 要求改写的部分
	net_module_run_state_get( MOD_CCID, (uint8_t *)ccid);
	memcpy(&AuthDa[idx], ccid, 20);
	idx += 20;

	AuthDa[idx++] = 0;	//string的结尾
	

	//消息体属性
	txLen = idx + 1;
	if (txLen < 13)
	{
		txLen = 13;
	}
	AuthDa[2] = (uint8_t)((txLen - 13) >> 8);
	AuthDa[3] = (uint8_t)(txLen - 13);

	//校验
	AuthDa[idx] = get_chk_xor(AuthDa, idx);
	idx ++;

    return mangpatio_msgtranslation(AuthDa,da, idx);
}
/** ****************************************************************************
* @remarks      uint16_t Authentication(uint8_t *da)
* @brief         鉴权
* @param[in]    无 
* @param[out]   *da缓存区	
* @return        缓存区长度
* @attention     
*******************************************************************************/
static uint16_t mangpatio_Authentication(uint8_t *da)
{
	uint8_t idx = 0;
	uint8_t term_id[4] = { 0 };
	uint8_t AuthDa[120] = { 0 };	
	uint8_t tmpDa[10] = { 0 };
	uint8_t reDa = 0;
	uint8_t ccid[20] = { 0 };
	n808_vehicle_data_t data_vehicle;
	char btmac[30] = { 0 };
	uint8_t btmac_hex[6]={0};
	uint8_t vin[17];
	uint8_t	auth_reason = 0;

	vehicle_data_now_get(&data_vehicle);
	//车辆管理ID
	memcpy(term_id, terid/*"\x01\x00\x07\x28"*/, 4);

	//Message_ID
	idx = 0;
	memcpy(&AuthDa[idx], "\x05\x01",2);
	idx += 2;

	//Message_Property, 放在最后
	//memcpy(&AuthDa[idx], "\x00\x1D",2);
	idx += 2;

	//Terminal_Num
	memcpy(&AuthDa[idx],terinf.ManageID, 6);
	idx += 6;

	//Message_Serial_Number
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum >> 8);
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum);
    mangpatioserialnum ++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}

	//Manage_ID  鉴权码
	memcpy(&AuthDa[idx],terinf.AuthCode, 4);
	idx += 4;

	//Term_ID 设备ID
	memcpy(&AuthDa[idx],term_id, 4);
	idx += 4;

	//科目编号  保留
	AuthDa[idx++] = MANGPATIO_AUTH_APPEND;//( uint8_t )( ( vehicle_type_data_get() << 7 ) | MANGPATIO_AUTH_APPEND );
	
	net_module_run_state_get( MOD_CCID, (uint8_t *)ccid);
	memcpy(&AuthDa[idx], ccid, 20);
	idx += 20;
	
	//当前版本号长度
	
	//放入当前软件版本号9字节
	AuthDa[idx++] = S_VER_LEN;
	if (S_VER_LEN <= 9)
	{
		memcpy(&AuthDa[idx], get_mcuversion(), S_VER_LEN);
		idx += S_VER_LEN;
	}
	
	//正在升级版本号长度，如果=0，则没有正在升级的版本号
	reDa = 0;//reCurUpData(tmpDa, 0);
	if (reDa == 0)
	{
		AuthDa[idx++] = 0;
	}
	else
	{
		AuthDa[idx++] = reDa;
		memcpy(&AuthDa[idx], tmpDa, reDa);
		idx += reDa;
	}
	
	//OBD升级的版本号
	AuthDa[idx++] = 5+12;
	memset(&AuthDa[idx], 0, 17);
	idx += 5 + 12;
	
	reDa = 0;//reCurUpData(tmpDa, 0xBD);
	if (reDa == 0)
	{
		AuthDa[idx++] = 0;
	}
	else
	{
		AuthDa[idx++] = reDa;
		memcpy(&AuthDa[idx], tmpDa, reDa);
		idx += reDa;
	}
	//追加包类型+长度+内容
	AuthDa[idx++] = 0x01;//VIN 
	memset(vin,0,17);
 
	if(vehicle_vin_get(vin) == pdPASS) 
	{
		AuthDa[idx++] = 0x11;//VIN
		memcpy(&AuthDa[idx],vin,17);
		idx += 17; 
	}	 
	else
	{
		AuthDa[idx++] = 0;
	} 
 
	AuthDa[idx++] = 0x02;//BLE MAC
	#if 1
	AuthDa[idx++] = 0x06;//
	reDa = ble_drive_macaddr_read(btmac);///保留
	gaoz_log_debug_printf(INFOR_MODE,"800c","ble_drive_macaddr_read : %s,%d\r\n",btmac,reDa);
	ASCIImac2Hex((uint8_t *)btmac,strlen(btmac),btmac_hex);
	AuthDa[idx++] = btmac_hex[5];//
	AuthDa[idx++] = btmac_hex[4];//
	AuthDa[idx++] = btmac_hex[3];//
	AuthDa[idx++] = btmac_hex[2];//
	AuthDa[idx++] = btmac_hex[1];//
	AuthDa[idx++] = btmac_hex[0];//
	#else
		AuthDa[idx++] = 0;
	#endif
	AuthDa[idx++] = 0x03;//固件
	{
	INT8 message[256] = {0};
	sprintf(message, "%s", fibo_get_sw_verno());
	gaoz_log_debug_printf(INFOR_MODE,"800c", "fibo_get_sw_verno[%s]]", message);
	AuthDa[idx++] = strlen(message);//length
	memcpy(&AuthDa[idx], message, strlen(message));
	idx += strlen(message);
	}
	AuthDa[idx++] = 0x04;//APP
	AuthDa[idx++] = strlen(gz_sw_verno);//length
	memcpy(&AuthDa[idx], gz_sw_verno, strlen(gz_sw_verno));
	idx += strlen(gz_sw_verno);

	//MCU配置表版本05

	AuthDa[idx++] = 0x06;//硬件类型
	AuthDa[idx++] = strlen(GAOZ_HW_TYPE1);//length
	memcpy(&AuthDa[idx], GAOZ_HW_TYPE1, strlen(GAOZ_HW_TYPE1));
	idx += strlen(GAOZ_HW_TYPE1);

	// 鉴权 重启原因
	AuthDa[idx++] = 0x0c;
	AuthDa[idx++] = 0x02;
	auth_reason = mangpatio_auth_reason_get();//鉴权原因 
	AuthDa[idx++] = auth_reason;
	AuthDa[idx++] = mangpatio_reset_reason;//重启原因
	

	gaoz_log_debug_printf(INFOR_MODE,"800b", "mangpatio auth info auth reason[%d] reset reason[%d]\r\n",auth_reason, mangpatio_reset_reason);

 
	AuthDa[2] = (uint8_t)((idx - 12) >> 8);
	AuthDa[3] = (uint8_t)(idx - 12);
	

	//校验
	AuthDa[idx] = get_chk_xor(AuthDa, idx);
	idx ++;

    return mangpatio_msgtranslation(AuthDa,da, idx);
}
/** ****************************************************************************
* @remarks      uint16_t SendHeart(uint8_t *da)
* @brief         心跳
* @param[in]     无
* @param[out]    *da缓存区
* @return       缓存区长度
* @attention     
*******************************************************************************/
static uint16_t mangpatio_SendHeart(uint8_t *da)
{
	uint8_t idx;
	uint8_t AuthDa[100];	
	uint16_t txLen;


	//Message_ID
	idx = 0;
	memcpy(&AuthDa[idx], "\x05\x06",2);
	idx += 2;

	//Message_Property,afterward
	//memcpy(&AuthDa[idx], "\x00\x05",2);
	idx += 2;

	//Terminal_Num
	memcpy(&AuthDa[idx],terinf.ManageID, 6);
	idx += 6;

	//Message_Serial_Number
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum >> 8);
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum);
	mangpatioserialnum ++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}

	//消息体属性
	txLen = idx + 1;
	if (txLen < 13)
	{
		txLen = 13;
	}
	AuthDa[2] = (uint8_t)((txLen - 13) >> 8);
	AuthDa[3] = (uint8_t)(txLen - 13);

	//校验
	AuthDa[idx] = get_chk_xor(AuthDa, idx);
	idx ++;

    return mangpatio_msgtranslation(AuthDa,da, idx);
}
/*
static uint16_t requestrsa(uint8_t *da)
{
	uint8_t idx;
	uint8_t AuthDa[100];	
	uint16_t txLen;


	//Message_ID
	idx = 0;
	memcpy(&AuthDa[idx], "\x05\x80",2);
	idx += 2;

	//Message_Property,afterward
	//memcpy(&AuthDa[idx], "\x00\x05",2);
	idx += 2;

	//Terminal_Num
	memcpy(&AuthDa[idx],terinf.ManageID, 6);
	idx += 6;

	//Message_Serial_Number
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum >> 8);
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum);
	mangpatioserialnum ++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}

	//消息体属性
	txLen = idx + 1;
	if (txLen < 13)
	{
		txLen = 13;
	}
	AuthDa[2] = (uint8_t)((txLen - 13) >> 8);
	AuthDa[3] = (uint8_t)(txLen - 13);

	//校验
	AuthDa[idx] = get_chk_xor(AuthDa, idx);
	idx ++;

    return msgtranslation(AuthDa,da, idx);
}*/
/** ****************************************************************************
* @remarks      uint16_t unregisterfromsys(uint8_t *da)
* @brief         终端注销
* @param[in]     无
* @param[out]    *da缓存区
* @return        缓存区长度
* @attention     
*******************************************************************************/
/*
static uint16_t unregisterfromsys(uint8_t *da)
{
	uint8_t idx;
	uint8_t AuthDa[100];	
	uint16_t txLen;


	//Message_ID
	idx = 0;
	memcpy(&AuthDa[idx], "\x00\x03",2);
	idx += 2;

	//Message_Property,afterward
	//memcpy(&AuthDa[idx], "\x00\x05",2);
	idx += 2;

	//Terminal_Num
	memcpy(&AuthDa[idx],terinf.ManageID, 6);
	idx += 6;

	//Message_Serial_Number
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum >> 8);
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum);
	mangpatioserialnum ++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}

	//消息体属性
	txLen = idx + 1;
	if (txLen < 13)
	{
		txLen = 13;
	}
	AuthDa[2] = (uint8_t)((txLen - 13) >> 8);
	AuthDa[3] = (uint8_t)(txLen - 13);

	//校验
	AuthDa[idx] = get_chk_xor(AuthDa, idx);
	idx ++;

    return msgtranslation(AuthDa,da, idx);
}
*/


 


/** ****************************************************************************
* @remarks      uint16_t mangpatio_positioninfo(uint8_t sel,uint8_t *da)
* @brief          位置信息
* @param[in]     sel 0:0504 1:0560
* @param[out]    *da缓存区
* @return        缓存区长度 
* @attention     
*******************************************************************************/
static uint16_t mangpatio_positioninfo(uint8_t sel,uint8_t *da)
{
    uint8_t idx;
	uint8_t AuthDa[500];	
	uint16_t txLen,payloadlen;
  
	//Message_ID
	idx = 0;
	if(sel == 0){
	memcpy(&AuthDa[idx], "\x05\x04",2);
	}else{
	memcpy(&AuthDa[idx], "\x05\x60",2);
	}
	idx += 2;

	//Message_Property,afterward
	idx += 2;

	//Terminal_Num
	if(memcmp(terinf.ManageID,"\x0\x0\x0\x0\x0\x0",6) == 0)
	{
		return 0;
	}
	memcpy(&AuthDa[idx],terinf.ManageID, 6);
	idx += 6;

	//Message_Serial_Number
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum >> 8);
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum);
	mangpatioserialnum ++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}

	//內容
	payloadlen = uploadposition(&AuthDa[idx]);
	idx += payloadlen;
	gaoz_log_debug_printf(INFOR_MODE,"800c","******gaoz_gnss_mileage_get<<<<<<<[%d],agnss_location_st_get[%d],runstate[%d],uploadposition[%d]\n", gaoz_gnss_mileage_get(),agnss_location_st_get(),mangpatio_ctrllock_state_get(),payloadlen);
#if 0
	//附加包0x30
	{
		uint32_t err_codecnt=0;
		err_codecnt = strlen((char *)gerr_code)&0xff;
		AuthDa[idx++] = 0x30;
		AuthDa[idx++] = err_codecnt;
		if(err_codecnt > 100)
		{
			err_codecnt = 100;			
		}
		if(err_codecnt != 0)
		{
			memcpy(&AuthDa[idx],gerr_code,err_codecnt);	
			idx += err_codecnt;
		}			
	}
#endif
	//附加包0x11
	uint32 sys_status = 0;
	AuthDa[idx++] = 0x11;
	AuthDa[idx++] = 8;	
	sys_status = gaoz_event_input_data_get(EVENT_SYSTEMSTATUS);
	u32toHex4(sys_status,&AuthDa[idx]);
	idx += 4;
	memset(&AuthDa[idx],0,4);
	idx += 4;
	
	/*附加包0x13 胎压数据*/
	AuthDa[idx++] = 0x13;
	AuthDa[idx++] = 10;
	idx += vehicle_tire_pressure_data_get( (uint8_t *)&AuthDa[idx] );

	/*附加包0x21 神州扩展状态字*/
	AuthDa[idx++] = 0x21; 
	idx += sz_ext_status_get( (uint8_t *)&AuthDa[idx] );
	
	/* 附加包 0x25 车辆新状态附加包 */
	AuthDa[idx++] = 0x25; 
	idx += vehicle_new_info_0504_25_info_get( (uint8_t *)&AuthDa[idx] );
	
	//消息体属性
	txLen = idx + 1;
	if (txLen < 13)
	{
		txLen = 13;
	}
	AuthDa[2] = (uint8_t)((txLen - 13) >> 8);
	AuthDa[3] = (uint8_t)(txLen - 13);

	//校验
	AuthDa[idx] = get_chk_xor(AuthDa, idx);
	idx ++;


    return mangpatio_msgtranslation(AuthDa,da, idx);
}

/** ****************************************************************************
* @remarks      void get_minorterm_para(uint8_t *da, uint16_t *cnt)
* @brief         获取上报参数表内容
* @param[in]     无
* @param[out]    *da缓存区，*cnt长度
* @return        无
* @attention     
*******************************************************************************/
static void get_minorterm_para(uint8_t *da, uint16_t *cnt)
{
	uint8_t tempDa[10];
	uint8_t tempSt[20];
	uint16_t Tx_Len;

	//终端ID
	memcpy(tempDa,terid, 4);
	Tx_Len = 0;
	memcpy(&da[Tx_Len], tempDa, 4);
	Tx_Len += 4;
	memcpy(&da[Tx_Len], "\x0\x0\x0", 3);
	Tx_Len += 3;
	//心跳间隔
	memcpy(&da[Tx_Len], terinf.heartBeatDly, 4);
	Tx_Len += 4;
	//主IP
	#if 0
	if(taginf.IP1Len < 15)
	{
		memcpy(&da[Tx_Len], taginf.IP1, taginf.IP1Len);
		Tx_Len += taginf.IP1Len;
		memset(tempSt,0,20);
		memcpy(&da[Tx_Len], tempSt, (15-taginf.IP1Len));
		Tx_Len += (15-taginf.IP1Len);
	}	
	else
	#endif
	da[Tx_Len++] = taginf.IP1Len;//
	{
		if(taginf.IP1Len != 0)
		{
			memcpy(&da[Tx_Len], taginf.IP1, taginf.IP1Len);
		}
		Tx_Len += taginf.IP1Len;
	}
	//副IP
	#if 0
	if(taginf.IP2Len < 15)
	{
		memcpy(&da[Tx_Len], taginf.IP2, taginf.IP2Len);
		Tx_Len += taginf.IP2Len;
		memset(tempSt,0,20);
		memcpy(&da[Tx_Len], tempSt, (15-taginf.IP2Len));
		Tx_Len += (15-taginf.IP2Len);	
	}
	else
	#endif
	da[Tx_Len++] = taginf.IP2Len;//
	{	
		if(taginf.IP2Len != 0)
		{
			memcpy(&da[Tx_Len], taginf.IP2, taginf.IP2Len);
		}
		Tx_Len += taginf.IP2Len;
	}
	//主端口号
	if(taginf.Port1Len < 5)
	{
		memcpy(&da[Tx_Len], taginf.Port1, taginf.Port1Len);
		Tx_Len += taginf.Port1Len;
		memset(tempSt,0,20);
		memcpy(&da[Tx_Len], tempSt, (5-taginf.Port1Len));
		Tx_Len += (5-taginf.Port1Len);
	}
	else
	{
		memcpy(&da[Tx_Len], taginf.Port1, taginf.Port1Len);
		Tx_Len += taginf.Port1Len;
	}

	//副端口号
	if(taginf.Port2Len < 5)
	{
		memcpy(&da[Tx_Len], taginf.Port2, taginf.Port2Len);
		Tx_Len += taginf.Port2Len;
		memset(tempSt,0,20);
		memcpy(&da[Tx_Len], tempSt, (5-taginf.Port2Len));
		Tx_Len += (5-taginf.Port2Len);
	}
	else
	{
		memcpy(&da[Tx_Len], taginf.Port2, taginf.Port2Len);
		Tx_Len += taginf.Port2Len;
	}
	
	//休眠时间上报间隔
	memcpy(&da[Tx_Len], terinf.idleReportMaxTime, 4);
	Tx_Len += 4;
	//正常时间上报间隔
	memcpy(&da[Tx_Len], terinf.workReportMaxTime, 4);
	Tx_Len += 4;
	//正常距离上报间隔
	memcpy(&da[Tx_Len], terinf.workReportMaxMeter, 4);
	Tx_Len += 4;
	//休眠距离上报间隔
	memcpy(&da[Tx_Len], terinf.idleReportMaxMeter, 4);
	Tx_Len += 4;
	//apn-node
	memset(tempSt,0,20);
	memcpy(&da[Tx_Len], tempSt, 15);
	Tx_Len += 15;
	//apnname
	memcpy(&da[Tx_Len], tempSt, 15);
	Tx_Len += 15;
	//apnpsw
	memcpy(&da[Tx_Len], tempSt, 15);
	Tx_Len += 15;
	//battime-on
	memcpy(&da[Tx_Len], tempSt, 4);
	Tx_Len += 4;
	//battime-off
	memcpy(&da[Tx_Len], tempSt, 4);
	Tx_Len += 4;
	//DNS
	da[Tx_Len++] = terinf.IP1Len;//
	if(terinf.IP1Len != 0)
	{
		memcpy(&da[Tx_Len], terinf.IP1, terinf.IP1Len);
	}
	Tx_Len += terinf.IP1Len;
	//DNS-port
	if(terinf.Port1Len < 5)
	{
		memcpy(&da[Tx_Len], terinf.Port1, terinf.Port1Len);
		Tx_Len += terinf.Port1Len;
		memset(tempSt,0,20);
		memcpy(&da[Tx_Len], tempSt, (5-terinf.Port1Len));
		Tx_Len += (5-terinf.Port1Len);
	}
	else
	{
		memcpy(&da[Tx_Len], terinf.Port1, terinf.Port1Len);
		Tx_Len += terinf.Port1Len;
	}
	//net-switch
	da[Tx_Len] = terinf.Mode|taginf.Mode;
	Tx_Len += 1;

	memset(tempSt,0,12);
	memcpy(&da[Tx_Len], tempSt, 12);
	Tx_Len += 12;
	
	*cnt = Tx_Len;
}
/*
static uint16_t rsasecret(uint8_t *da)
{
	uint8_t idx;
	uint8_t AuthDa[512];	
	uint16_t txLen;
	uint8_t respda[256]={0};
	//uint16_t ter_cnt;
	int ret = 0;

	//Message_ID
	idx = 0;
	memcpy(&AuthDa[idx], "\x05\x81",2);
	idx += 2;

	idx += 2;
	//Terminal_Num
	memcpy(&AuthDa[idx],terinf.ManageID, 6);
	idx += 6;

	//Message_Serial_Number
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum >> 8);
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum);
	mangpatioserialnum ++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}

	AuthDa[idx++] = (uint8_t)(0>>8);
	AuthDa[idx++] = (uint8_t)0;
	//应答ID
	AuthDa[idx++] = 0x95;
	AuthDa[idx++] = 0x81;
	
	ret = file_termpara_read(RSAPARA,(INT8 *)respda);
	if(ret < 0){
		#if 1
		gaoz_log_debug_printf(INFOR_MODE,"800c","filelog_init(RSAPARA)= %d", ret);
		#endif
	}else{
		#if 1
		gaoz_log_debug_printf(INFOR_MODE,"800c","filelog_init(RSAPARA)= %d", ret-1);
		#endif
		memcpy(&AuthDa[idx], respda, ret-1);
		idx += ret-1;
	}

	//消息体属性
	txLen = idx + 1;
	if (txLen < 13)
	{
		txLen = 13;
	}
	AuthDa[2] = (uint8_t)((txLen - 13) >> 8);
	AuthDa[3] = (uint8_t)(txLen - 13);

	//校验
	AuthDa[idx] = get_chk_xor(AuthDa, idx);
	idx ++;

    return msgtranslation(AuthDa,da, idx);
}
*/
/** ****************************************************************************
* @remarks      uint16_t parameter(uint8_t *da)
* @brief         终端参数表
* @param[in]     无
* @param[out]    *da缓存区
* @return        缓存区长度  
* @attention     
*******************************************************************************/
static uint16_t mangpatio_parameter(national808_net_devres_msg_t *res_msg,uint8_t *da)
{
	uint8_t idx;
	uint8_t AuthDa[512];	
	uint16_t txLen;
	uint8_t respda[512];
	uint16_t ter_cnt;

	//Message_ID
	idx = 0;
	memcpy(&AuthDa[idx], "\x05\x90",2);
	idx += 2;

	idx += 2;

	//Terminal_Num
	memcpy(&AuthDa[idx],terinf.ManageID, 6);
	idx += 6;

	//Message_Serial_Number
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum >> 8);
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum);
	mangpatioserialnum ++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}

	respda[0] = (uint8_t)(res_msg->respondsn>>8);
	respda[1] = (uint8_t)(res_msg->respondsn);
	//应答ID
	respda[2] = 0x95;
	respda[3] = 0x90;
	get_minorterm_para(&respda[4],&ter_cnt);//获取参数表
	memcpy(&AuthDa[idx], respda, ter_cnt+4);
	idx += ter_cnt+4;
	//消息体属性
	txLen = idx + 1;
	if (txLen < 13)
	{
		txLen = 13;
	}
	AuthDa[2] = (uint8_t)((txLen - 13) >> 8);
	AuthDa[3] = (uint8_t)(txLen - 13);

	//校验
	AuthDa[idx] = get_chk_xor(AuthDa, idx);
	idx ++;

    return mangpatio_msgtranslation(AuthDa,da, idx);
}
/** ****************************************************************************
* @remarks      uint16_t property(uint8_t *da)
* @brief         终端属性
* @param[in]     无
* @param[out]    *da缓存区
* @return        缓存区长度 
* @attention     
*******************************************************************************/
static uint16_t mangpatio_property(uint8_t *da)
{
	uint8_t idx;
	uint8_t AuthDa[128];	
	uint16_t txLen;
	uint8_t term_id[4];
	uint8_t ccid[20]={0};

	//Message_ID
	idx = 0;
	memcpy(&AuthDa[idx], "\x01\x07",2);
	idx += 2;

	idx += 2;

	//Terminal_Num
	memcpy(&AuthDa[idx],terinf.ManageID, 6);
	idx += 6;

	//Message_Serial_Number
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum >> 8);
	AuthDa[idx++] = (uint8_t)(mangpatioserialnum);
	mangpatioserialnum ++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}
	//终端类型
	AuthDa[idx++] = 0;
	AuthDa[idx++] = (uint8_t)TERNIMAL_TYPE;
	
	//制造商ID
	memcpy(&AuthDa[idx], manufactureID, 5);
	idx += 5;

	//终端型号
	//memcpy(&AuthDa[19], terminal_type, 20);
	net_module_run_state_get( MOD_CCID, (uint8_t *)ccid);	
	memcpy(&AuthDa[idx], ccid, 20);	
	idx += 20;
	
	//终端ID
	memcpy(term_id,terid, 4);
	memcpy(&AuthDa[idx], term_id, 4);
	idx += 4;
	memset(&AuthDa[idx], 0, 3);
	idx += 3;


	//终端SIM 卡ICCID，作废
	//Manage_ID  鉴权码
	memcpy(&AuthDa[idx], terinf.AuthCode, 4);
	idx += 4;
	//凑数
	memset(&AuthDa[idx], 0x31, 10-4);
	idx += 6;

	//终端硬件版本号长度
	AuthDa[idx++] = H_ver_len;

	//终端硬件版本号
	memcpy(&AuthDa[idx], H_ver, H_ver_len);

	idx += H_ver_len;

	//终端固件版本号长度
	AuthDa[idx++] = S_VER_LEN;
	//终端固件版本号
	//memcpy(&AuthDa[idx], S_VER, S_VER_LEN);
	if (S_VER_LEN <= 9)
	{
		memcpy(&AuthDa[idx], get_mcuversion(), S_VER_LEN);
		idx += S_VER_LEN;
	}
	//GNSS 模块属性
	AuthDa[idx++] = 0x03;

	//通信模块属性
	AuthDa[idx++] = 0x01;

	//增加OBD的版本号(固件)
	{
		INT8 message[256] = {0};
		sprintf(message, "%s", fibo_get_sw_verno());
		gaoz_log_debug_printf(INFOR_MODE,"800c", "fibo_get_sw_verno[%s]]", message);
		AuthDa[idx++] = strlen(message);//length
		memcpy(&AuthDa[idx], message, strlen(message));
		idx += strlen(message);
	}
	//app version
	AuthDa[idx++] = strlen(gz_sw_verno);//length
	memcpy(&AuthDa[idx], gz_sw_verno, strlen(gz_sw_verno));
	idx += strlen(gz_sw_verno);

	//消息体属性
	txLen = idx + 1;
	if (txLen < 13)
	{
		txLen = 13;
	}
	AuthDa[2] = (uint8_t)((txLen - 13) >> 8);
	AuthDa[3] = (uint8_t)(txLen - 13);

	//校验
	AuthDa[idx] = get_chk_xor(AuthDa, idx);
	idx ++;

    return mangpatio_msgtranslation(AuthDa,da, idx);
}

/** ****************************************************************************
* @remarks      uint16_t generalrespond(mangpatio_net_devres_msg_t *res_msg,uint8_t *da)
* @brief          通用应答
* @param[in]     mangpatio_net_devres_msg_t网络应答结果
* @param[out]    *da缓存区
* @return        缓存区长度  
* @attention     
*******************************************************************************/
static uint16_t mangpatio_generalrespond(national808_net_devres_msg_t *res_msg,uint8_t *da)
{
	uint8_t tmpDa[30];
	
	//消息头，共用部分		
	tmpDa[0] = 0x05;
	tmpDa[1] = 0x20;
	
	//car ID
	if(res_msg->respondcmd == 0x9571)
	{
		memcpy(&tmpDa[4], currinf.ManageID, 6);
	}
	else
	{
		memcpy(&tmpDa[4], terinf.ManageID, 6);
	}

	//流水号
	tmpDa[10] = (uint8_t)(mangpatioserialnum >> 8);
	tmpDa[11] = (uint8_t)(mangpatioserialnum & 0xff);
	mangpatioserialnum++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}
	//应答流水号
	tmpDa[12] = (uint8_t)(res_msg->respondsn>>8);
	tmpDa[13] = (uint8_t)(res_msg->respondsn);

	//应答ID
	tmpDa[14] = (uint8_t)(res_msg->respondcmd>>8);
	tmpDa[15] = (uint8_t)(res_msg->respondcmd);
	//结果
	tmpDa[16] = res_msg->respondsta;
	//属性
	tmpDa[2] = 0;
	tmpDa[3] = 5;
	//校验
	tmpDa[17] = get_chk_xor(tmpDa,17);
	
    return mangpatio_msgtranslation(tmpDa,da,18);
}
/** ****************************************************************************
* @remarks      uint16_t rsarespond(national808_net_devres_msg_t *res_msg,uint8_t *da)
* @brief         查询密钥
* @param[in]     national808_net_devres_msg_t网络应答结果
* @param[out]    *da缓存区
* @return        缓存区长度 
* @attention     
*******************************************************************************/
static uint16_t mangpatio_rsarespond(national808_net_devres_msg_t *res_msg,uint8_t *da)
{
	uint8_t tmpDa[500];
	uint16_t index = 0;
	//消息头，共用部分		
	tmpDa[index++] = 0x05;
	tmpDa[index++] = 0x81;
	
	//car ID
	index = 4;
	memcpy(&tmpDa[index], terinf.ManageID, 6);
	index += 6;
	//流水号
	tmpDa[index++] = (uint8_t)(mangpatioserialnum >> 8);
	tmpDa[index++] = (uint8_t)(mangpatioserialnum & 0xff);
	mangpatioserialnum++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}
	//消息头，共用部分		
	tmpDa[index++] = (uint8_t)(res_msg->respondsn>>8);
	tmpDa[index++] = (uint8_t)(res_msg->respondsn);
	//应答ID
	tmpDa[index++] = (uint8_t)(res_msg->respondcmd>>8);
	tmpDa[index++] = (uint8_t)(res_msg->respondcmd);
	//公钥和长度
	tmpDa[index++] = (u8)( strlen((char *)bluetooth_getrsa())>>8);
	tmpDa[index++] = (u8)( strlen((char *)bluetooth_getrsa()));
	memcpy(&tmpDa[index], bluetooth_getrsa(), strlen((char *)bluetooth_getrsa()));
	index += strlen((char *)bluetooth_getrsa());
	//蓝牙名称和长度
	tmpDa[index++] = strlen((char *)bluetooth_getname());
	memcpy(&tmpDa[index], bluetooth_getname(), strlen((char *)bluetooth_getname()));
	index += strlen((char *)bluetooth_getname());
	//属性
	tmpDa[2] = (uint8_t)((index - 12) >> 8);
	tmpDa[3] = (uint8_t)(index - 12);
	//校验
	tmpDa[index] = get_chk_xor(tmpDa,index);
	index++;
	
    return mangpatio_msgtranslation(tmpDa,da,index);
}
/** ****************************************************************************
* @remarks      uint16_t qxwzrespond(national808_net_devres_msg_t *res_msg,uint8_t *da)
* @brief         查询密钥
* @param[in]     national808_net_devres_msg_t网络应答结果
* @param[out]    *da缓存区
* @return        缓存区长度 
* @attention     
*******************************************************************************/
static uint16_t qxwzrespond(national808_net_devres_msg_t *res_msg,uint8_t *da)
{
	uint8_t tmpDa[200];
	uint16_t index = 0;
	qxwz_save_cfg_infor_t qwxz_cfg_t = {0};
	uint32_t ret = pdFAIL;
	//消息头，共用部分		
	tmpDa[index++] = 0x06;
	tmpDa[index++] = 0x36;
	
	//car ID
	index = 4;
	memcpy(&tmpDa[index], terinf.ManageID, 6);
	index += 6;
	//流水号
	tmpDa[index++] = (uint8_t)(mangpatioserialnum >> 8);
	tmpDa[index++] = (uint8_t)(mangpatioserialnum & 0xff);
	mangpatioserialnum++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}
	//消息头，共用部分		
	tmpDa[index++] = (uint8_t)(res_msg->respondsn>>8);
	tmpDa[index++] = (uint8_t)(res_msg->respondsn);
	//应答ID
	tmpDa[index++] = (uint8_t)(res_msg->respondcmd>>8);
	tmpDa[index++] = (uint8_t)(res_msg->respondcmd);
	//key和长度
	ret = gaoz_qxwz_inject_cfg_save_flash_read(&qwxz_cfg_t);		
	if(ret == pdFAIL)
	{
		gaoz_log_debug_printf(INFOR_MODE,"800c","gaoz_qxwz_inject_cfg_save_flash_read fail\n");
	}	
	else
	{
		tmpDa[index++] = strlen((char *)qwxz_cfg_t.app_key);
		if(strlen((char *)qwxz_cfg_t.app_key) != 0)
		{
			memcpy(&tmpDa[index], qwxz_cfg_t.app_key, strlen((char *)qwxz_cfg_t.app_key));
		}
		index += strlen((char *)qwxz_cfg_t.app_key);

		tmpDa[index++] = strlen((char *)qwxz_cfg_t.app_secret);
		if(strlen((char *)qwxz_cfg_t.app_secret) != 0)
		{
			memcpy(&tmpDa[index], qwxz_cfg_t.app_secret, strlen((char *)qwxz_cfg_t.app_secret));
		}
		index += strlen((char *)qwxz_cfg_t.app_secret);

		tmpDa[index++] = strlen((char *)qwxz_cfg_t.device_id);
		if(strlen((char *)qwxz_cfg_t.device_id) != 0)
		{
			memcpy(&tmpDa[index], qwxz_cfg_t.device_id, strlen((char *)qwxz_cfg_t.device_id));
		}
		index += strlen((char *)qwxz_cfg_t.device_id);

		tmpDa[index++] = strlen((char *)qwxz_cfg_t.device_type);
		if(strlen((char *)qwxz_cfg_t.device_type) != 0)
		{
			memcpy(&tmpDa[index], qwxz_cfg_t.device_type, strlen((char *)qwxz_cfg_t.device_type));
		}
		index += strlen((char *)qwxz_cfg_t.device_type);
	}
	//属性
	tmpDa[2] = (uint8_t)((index - 12) >> 8);
	tmpDa[3] = (uint8_t)(index - 12);
	//校验
	tmpDa[index] = get_chk_xor(tmpDa,index);
	index++;
	
    return mangpatio_msgtranslation(tmpDa,da,index);
}
/** ****************************************************************************
* @remarks      uint16_t updateresult(update_result_t *upslt,uint8_t *da)
* @brief         升级结果
* @param[in]     *upslt升级结果队列
* @param[out]    *da缓存区
* @return        缓存区长度 
* @attention     
*******************************************************************************/
static uint16_t mangpatio_updateresult(update_result_t *upslt,uint8_t *da)
{
	uint8_t tmpDa[30];
	
	//消息头，共用部分		
	tmpDa[0] = 0x01;
	tmpDa[1] = 0x08;
	
	//car ID
	memcpy(&tmpDa[4], terinf.ManageID, 6);

	//流水号
	tmpDa[10] = (uint8_t)(mangpatioserialnum >> 8);
	tmpDa[11] = (uint8_t)(mangpatioserialnum & 0xff);
	mangpatioserialnum++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}	
	//升级类型
	tmpDa[12] = (uint8_t)(upslt->update_types);
	//结果
	tmpDa[13] = upslt->update_result;
	//属性
	tmpDa[2] = 0;
	tmpDa[3] = 2;
	//校验
	tmpDa[14] = get_chk_xor(tmpDa,14);
	
    return mangpatio_msgtranslation(tmpDa,da,15);
}

/** ****************************************************************************
* @remarks       uint16_t lockrespond(national808_net_devres_msg_t *res_msg,uint8_t *da)
* @brief         异步锁上报
* @param[in]     national808_net_devres_msg_t网络应答结果
* @param[out]    *da缓存区
* @return        缓存区长度 
* @attention     
*******************************************************************************/	
static uint16_t mangpatio_lockrespond(uint8_t *da, national808_ctrllock_msg_t *res_msg)
{
	uint8_t tmpDa[30];
	uint16_t idx = 0;
	
	//消息头，共用部分		
	tmpDa[idx++] = (uint8_t)(res_msg->lock_res.respondcmd>>8)&0x0f;
	tmpDa[idx++] = (uint8_t)(res_msg->lock_res.respondcmd&0xff);
	
	//car ID
	idx = 4;
	memcpy(&tmpDa[idx], terinf.ManageID, 6);
	idx += 6;
	//流水号
	tmpDa[idx++] = (uint8_t)(mangpatioserialnum >> 8);
	tmpDa[idx++] = (uint8_t)(mangpatioserialnum & 0xff);
	mangpatioserialnum++;
	if (mangpatioserialnum > 0x8000)
	{
		mangpatioserialnum = 1;
	}
	//应答流水号
	tmpDa[idx++] = (uint8_t)(res_msg->lock_res.respondsn>>8);
	tmpDa[idx++] = (uint8_t)(res_msg->lock_res.respondsn&0xff);

	//应答ID
	tmpDa[idx++] = (uint8_t)(res_msg->lock_res.respondcmd>>8); //0x95;
	tmpDa[idx++] = (uint8_t)(res_msg->lock_res.respondcmd&0xff);// 0x63;
	//结果
	tmpDa[idx++] = res_msg->lock_res.respondsta;
	if(res_msg->lock_res.respondsta == 1)
	{
		tmpDa[idx++] = (uint8_t)(res_msg->lock_err >> 8);
		tmpDa[idx++] = (uint8_t)(res_msg->lock_err & 0xff);	
	}
	//属性
	if(res_msg->lock_res.respondsta == 0)
	{
		tmpDa[2] = 0;
		tmpDa[3] = 5;
	}
	else
	{
		tmpDa[2] = 0;
		tmpDa[3] = 7;		
	}
	//校验
	tmpDa[idx] = get_chk_xor(tmpDa,idx);
	idx ++;

    return mangpatio_msgtranslation(tmpDa,da,idx);
}
	
/** ****************************************************************************
* @remarks      uint16_t msgtranslation(uint8_t *da,uint8_t *txbuf, uint16_t len)
* @brief          转译
* @param[in]     *da缓存区,len缓存区长度 
* @param[out]    *txbuf缓存区
* @return        txbuf缓存区长度  
* @attention     
*******************************************************************************/
static uint16_t mangpatio_msgtranslation(uint8_t *da,uint8_t *txbuf, uint16_t len)
{
	uint16_t j = 0,txindex = 0, encrypt_len = 0;
	uint8_t pdata[1024 + 30]={0};
	uint8_t *data = da;
#if( MANGPATIO_USER_AES_ENCRYPT == 1 )
	uint16_t serial_number = 0;
#endif
	if (len < 1024)
	{
		memcpy(data, da, len);
	}
	else
	{
		return 0;
	}
	/*原始数据进行AES加密*/	
	encrypt_len = len;
#if( MANGPATIO_USER_AES_ENCRYPT == 1 )
	serial_number = ( data[10] * 256 + data[11] );
	if( mangpatio_encrypt_package( (uint8_t *)data, (uint16_t *)&encrypt_len, serial_number ) == pdPASS )
	{
		mangpatio_encrypt_package_shell( (uint8_t *)data, (uint16_t *)&encrypt_len );	
	}
#endif
	txindex = 0;
	pdata[txindex ++] = 0x7e;
	//消息转译
	for(j = 0;j < encrypt_len; j ++)
	{
		if(*data == 0x7E)
		{
			pdata[txindex ++] = 0x7D;
			pdata[txindex ++] = 0x02;
		}
		else if(*data == 0x7D)
		{
			pdata[txindex ++] = 0x7D;
			pdata[txindex ++] = 0x01;
		} 
		else 
		{
			pdata[txindex ++] = *data;
		}

		data ++;
	}

	pdata [txindex ++] = 0x7E;
    memcpy(txbuf,pdata, txindex);
    return txindex;
}

/** ****************************************************************************
* @remarks       uint8_t initTerminalParaAll(uint8_t * ManageID,uint8_t *da,uint16_t *len)
* @brief          初始参数表
* @param[in]      ManageID设备id
* @param[out]    *da缓存区,*len缓存区长度
* @return         0成功 
* @attention     
*******************************************************************************/
static uint8_t mangpatio_initTerminalParaAll(uint8_t * ManageID,uint8_t *da,uint16_t *len)
{
	uint8_t check;
	uint16_t i;
	
	//因为之前老版本都是1~3，新版本中如果发现参数表校验错误，会重写车辆ID，从原来的4有效字节，变成6字节。
	terinf.Subject = 0x05;	//科目
	terinf.IP1Len = 0x12;	//主IP长度
	memcpy(terinf.IP1, "800b.zykuaiche.com", strlen("800b.zykuaiche.com"));	//主IP
	terinf.Port1Len = 0x05;	//主端口长度
	memcpy(terinf.Port1,"\x34\x30\x30\x30\x30", 5);		//主端口
	terinf.IP2Len = 0x12;	//备用IP长度 
	memcpy(terinf.IP2, "800b.zykuaiche.com", strlen("800b.zykuaiche.com"));	//备用IP	
	terinf.Port2Len = 0x05;	//备用端口长度
	memcpy(terinf.Port2, "\x34\x30\x30\x30\x30", 5);		//备用端口

	terinf.Mode = 0x20;	//工作模式 01h关闭本地连接，10h打开本地连接，02h关闭第二路连接，20h打开第二路连接
	terinf.Voltage = 0x01;	//电瓶电压 "1-12V 2-24V 3-36V",17
	terinf.SpeedLimit = 0x78;	//限速
	memcpy(terinf.ManageID, ManageID, 6);	//车辆管理ID

	terinf.AuthCodeFlag = 0;	//鉴权码，如果为A1代表已存储鉴权码，其它没有
	memset(terinf.AuthCode, 0xff, 4);	//4字节鉴权码


	memcpy(terinf.heartBeatDly, "\x0\x0\x0\x1E", 4); //终端心跳发送间隔，单位为秒（s）默认120 s


	memcpy(terinf.idleReportMaxTime, "\x0\x0\x02\x58", 4);	//休眠时汇报时间间隔，单位为秒（s） 	//不教学
	memcpy(terinf.workReportMaxTime, "\x0\x0\x00\x3c", 4);	//缺省时间汇报间隔，单位为秒（s）		//教学中
	memcpy(terinf.idleReportMaxMeter, "\x0\x0\x0\xc8", 4);	//休眠时汇报距离间隔，单位为米（m），>0
	memcpy(terinf.workReportMaxMeter, "\x0\x0\x0\xc8", 4);	//缺省距离汇报间隔

	terinf.crashTime = 2;	//碰撞报警参数设置：b7-b0： 碰撞时间，单位4ms

	terinf.crashG = 35;		//碰撞加速度，单位 0.1g
	
	//侧翻报警参数设置：侧翻角度，单位1度，默认为30度
	memcpy(terinf.CarAngle,"\x0\x1e",2);
	
	//车辆里程表读数，1/10km
	memset(terinf.CarKm, 0, 4);

	terinf.Check = 0;

	check=0;
	for(i=0;i<sizeof(mangpatio_info_t)-1;i++)
	{
		check^=*((uint8_t*)&terinf+i);		
	}
	*((uint8_t*)&terinf+sizeof(mangpatio_info_t)-1)=check;
	if((da != NULL) && (len != NULL))
	{
		memcpy(da,&terinf,sizeof(mangpatio_info_t));
		*len = sizeof(mangpatio_info_t);
	}
	return 0;
}

/** ****************************************************************************
* @remarks       int readTerminalPara(const INT8 *filename)
* @brief          读取参数表
* @param[in]      *filename文件路径
* @param[out]     无
* @return         <0失败 0成功
* @attention     
*******************************************************************************/
static int mangpatio_readTerminalPara(const INT8 *filename)
{
	uint8_t check = 0;

	int fd = fibo_file_open(filename, FS_O_RDONLY);
    if (fd < 0)
    {
        return -1;
    }
    char *temp = (char *)fibo_malloc(1024*1);
    if (temp == NULL)
    {
		return -1;
        //OSI_LOGI(0, "readTerminalPara %u", (unsigned int)temp);
    }
	memset(temp,0,1024);
	INT32 file_offset = fibo_file_seek(fd,8,FS_SEEK_CUR); 
	if(file_offset < 0)
	{
		//OSI_LOGI(0, "fibo_file_seek fail");
		return -1;
	}
    int ret = fibo_file_read(fd, (UINT8 *)temp, sizeof(mangpatio_info_t));
    if (ret < 0)
    {
        fibo_file_close(fd);
        free(temp);
		return -1;
    }
	if(strlen(temp) == 0)
    {
        fibo_file_close(fd);
        free(temp);
		return -1;
    }
	check = get_chk_xor((uint8_t *)temp,sizeof(mangpatio_info_t));
	if(check != 0)
	{
        fibo_file_close(fd);
        free(temp);	
		return -1;
	}
	memcpy(&terinf,temp,sizeof(mangpatio_info_t));
    fibo_file_close(fd);
    free(temp);

    return 0;
}
/** ****************************************************************************
* @remarks       static int mangpatio_saveserviceloadpara(uint8_t *pDa,uint8_t type,uint8_t *pDa2)
* @brief         存储配置
* @param[in]     无
* @param[out]    无
* @return        0失败 1成功
* @attention
*******************************************************************************/
static int mangpatio_saveserviceloadpara(uint8_t *pDa,uint8_t type,uint8_t *pDa2)
{
	uint8_t check;
	int ret = -1;
	mangpatio_info_t current_info={0};

    char *temp = (char *)gaoz_fibo_malloc(1024*1);
    if (temp == NULL)
    {
       return 0;/* 失败 */
    }
	memcpy(&current_info,&terinf,sizeof(mangpatio_info_t));
    switch (type)
	{
		case 1:	//注册的消息，保存鉴权码
			memcpy(current_info.AuthCode, pDa, 4);
			current_info.AuthCodeFlag = 0xA1;
			/*保存通信密钥和存储数据密钥*/
			current_info.rs[0] = 0x01;
			memcpy( (uint8_t *)&current_info.rs[1], (uint8_t *)&pDa[4], 16 );		/*通信密钥*/
			current_info.rs[17] = 0x01;
			memcpy( (uint8_t *)&current_info.rs[18], (uint8_t *)&pDa[20], 16 );	/*存储密钥*/
			break;
		case 2:	//终端注销状态
			current_info.AuthCodeFlag = 0xff;
			memset(current_info.AuthCode, 0xff, 4);
			memset((uint8_t *)current_info.rs, 0x00, sizeof( current_info.rs ));
			break;
		case 3:	//存主IP
			memset(current_info.IP1, 0, sizeof(current_info.IP1));
			current_info.IP1Len = *pDa2;
			memcpy(current_info.IP1, pDa, current_info.IP1Len);
			break;
		case 4:	//存主端口
			memset(current_info.Port1, 0, sizeof(current_info.Port1));
			current_info.Port1Len = *pDa2;
			memcpy(current_info.Port1, pDa, current_info.Port1Len);
			break;
		case 5:	//存副IP
			memset(current_info.IP2, 0, sizeof(current_info.IP2));
			current_info.IP2Len = *pDa2;
			memcpy(current_info.IP2, pDa, current_info.IP2Len);
			break;
		case 6:	//存副端口
			memset(current_info.Port2, 0, sizeof(current_info.Port2));
			current_info.Port2Len = *pDa2;
			memcpy(current_info.Port2, pDa, current_info.Port2Len);
			break;
		//设置电瓶电压 "1-12V 2-24V 3-36V",17
		case 7:
			if ( (*pDa >= '1') && (*pDa <= '3') )
			{
				current_info.Voltage = *pDa - '0';
			}
			else
			{
				current_info.Voltage = 1;
			}
			break;

		//请输入限速值
		case 8:
			if (*pDa >= 130)
			{
				*pDa = 130;
			}
		
			current_info.SpeedLimit = *pDa;
			break;
		//输入新ID号
		case 9:
			memcpy(current_info.ManageID, pDa, 6);		//20160125 原来是4字节，改成6字节
			break;
		//请输入拍照间隔
		case 10:
			
			break;
			//请输入新手机号
		case 11:
		
			break;
		//摄像头故障处理， 0-不启用，1-启用
		case 12:
			
			break;

		//语音提示开关  1-开  0-关
		case 13:
			
			break;

		case 14:
			
			break;

		case 15:
			current_info.Subject = (*pDa & 0x0F);
			break;

		case 16:
			
			break;
		
		case 17:
			
			break;

		case 18:

			break;

		case 19:

			break;			
			
		case 20:

			break;
		case 21:	//APN名称
			
			break;
		case 22:	//APN用户名称
		
			break;
		case 23:	//APN密码
			
			break;
		case 24:	//点火时汇报电池信息时间间隔

			break;
		case 25:	//熄火时汇报电池信息时间间隔

			break;

		}
		
		check = get_chk_xor((uint8_t*)(&current_info), sizeof(mangpatio_info_t)-1);
		current_info.Check = check;
		gaoz_log_debug_printf(INFOR_MODE,"800c","saveserviceloadpara <--%s,%d",&current_info,sizeof(mangpatio_info_t));
		memcpy(temp,terid,4);	
		memcpy(&temp[4],"EDTT",4);
		memcpy(&temp[8],&current_info,sizeof(mangpatio_info_t));
		ret = file_termpara_write(MANGPATIOR_FILE,temp,sizeof(mangpatio_info_t)+8);
		if(ret < 0)
		{
			//default;
			mangpatio_initTerminalParaAll(terid,NULL,NULL);//default;
			gaoz_fibo_free(temp);	
			return 0;
		}
		/*ret = flash_spi_write_data(SF_SYS_PARA_FLAG_ADDR,(uint8_t *)temp,sizeof(terminal_info_t)+8);
		if(ret == pdPASS)
		{
			gaoz_log_debug_printf(INFOR_MODE,"800c",">>SF_SYS_PARA_FLAG_ADDR Okay<<");	
		}
		ret = flash_spi_write_data(SF_SYS_PARA_BACKUP_ADDR,(uint8_t *)temp,sizeof(terminal_info_t)+8);
		if(ret == pdPASS)
		{
			gaoz_log_debug_printf(INFOR_MODE,"800c",">>SF_SYS_PARA_BACKUP_ADDR Okay<<");
		}*/
		memcpy(&terinf,&current_info,sizeof(mangpatio_info_t));
		gaoz_fibo_free(temp);	
		return 1;
}
/** ****************************************************************************
* @remarks       uint32_t saveminorip(mangpatio_info_t *info,uint8_t flag)
* @brief         存储参数表
* @param[in]     参数表，标记是否需要重启网络
* @param[out]    无
* @return        1失败 0成功
* @attention
*******************************************************************************/
uint32_t saveminorip(mangpatio_info_t *info,uint8_t flag)
{
	UINT8 check = 0;
	int ret = 0;

	if(info == NULL)
	{
		return 1;
	}

	char *temp = (char *)gaoz_fibo_malloc(1024*1);
    if (temp == NULL)
    {
        return 1;
    }
	memset(temp,0,1024);

	check = get_chk_xor((uint8_t*)(info), sizeof(mangpatio_info_t)-1);
	info->Check = check;
	memcpy(temp,terid,4);	
	memcpy(&temp[4],"EDTT",4);
	memcpy(&temp[8],info,sizeof(mangpatio_info_t));
	ret = file_termpara_write(MANGPATIOR_FILE,temp,sizeof(mangpatio_info_t)+8);
	if(ret < 0)
	{
		gaoz_fibo_free(temp);	
		return 1;
	}	

	memcpy(&terinf,info,sizeof(mangpatio_info_t));
	if(flag == 2)
	{
		/* 重启设备 */
		mangpatio_rst = NET_RST_END;
	}
	gaoz_fibo_free(temp);
	return 0;
}
/** ****************************************************************************
* @remarks       uint8_t setParafromservice(uint8_t * da, uint16_t len)
* @brief         存储参数表
* @param[in]     无
* @param[out]    无
* @return        1失败 0成功
* @attention
*******************************************************************************/
static uint8_t mangpatio_setParafromservice(uint8_t * da, uint16_t len)
{
	UINT32 paraID = 0;
	UINT8 ParaLen = 0;
	UINT8 * pParaDa;
	UINT16 len_cur; //保证参数在总长度内
	UINT8 ParaCnt;

	UINT8 para_cur;	
	int ret = 0;
	INT8 rebootflag = 0;
	INT8 pTmpDa[128]={0};

	UINT8 uint8_tTmpDa = 0;
	UINT8 check = 0;
	terminal_info_t	major_info={0};
	mangpatio_info_t current_info={0};
	uint32_t ulTmpDa = 0;
	uint8_t vinbuf[18];

	char *temp = (char *)gaoz_fibo_malloc(1024*1);
    if (temp == NULL)
    {
        return 1;
    }

	/**/
	memcpy(&current_info,&terinf,sizeof(mangpatio_info_t));//minor
	memcpy(&major_info,&taginf,sizeof(terminal_info_t));//major
	ParaCnt = *da;
	len_cur = 1;
	pParaDa = &da[1];

	//for(int i = 0,j = 0,k = 0;i < len;i += (5*k+j))
	for (para_cur=0;para_cur<ParaCnt;para_cur++)
	{
		paraID = hex4CombineOne(pParaDa);
		pParaDa += 4;
		ParaLen = pParaDa[0];
		pParaDa ++;
		len_cur += 5;
		len_cur += ParaLen;
		if (len_cur > len)
		{
			ret = 1;
			break;
		}
		/*
		paraID = hex4CombineOne(&da[i]);
		j += da[i+4];
		k++;
		ParaLen = da[i+4];
		gaoz_log_debug_printf(INFOR_MODE,"800c","paraID=%x,ParaLen=%d,j=%d,k=%d,len=%d\r\n",paraID,ParaLen,j,k,len);
		*/
		if(paraID == 0x0001)
		{
			if(ParaLen != 4)
			{
					ret = 1;
					break;					
			}
			memcpy(current_info.heartBeatDly, pParaDa, ParaLen);
			#if 1
			gaoz_log_debug_printf(INFOR_MODE,"800c","current_info.heartBeatDly=%d",hex4CombineOne(current_info.heartBeatDly));
			#endif
		}
		else if(paraID == 0x0013)
		{
			if ((judgeIpType(pParaDa, ParaLen) == 0) ||
				(get_valid_domain_name((char *)pParaDa, ParaLen) == 0))
			{
				memset(major_info.IP1,0,sizeof(major_info.IP1));
				memcpy(major_info.IP1, pParaDa, ParaLen);
				major_info.IP1Len = ParaLen;
				rebootflag = 2;
			}
			else
			{
				ret = 1;
				break;	
			}
			#if 1
			gaoz_log_debug_printf(INFOR_MODE,"800c","major_info.IP1=%s,%d",major_info.IP1,major_info.IP1Len);
			#endif
		}
		else if(paraID == 0x0017)
		{
			if ((judgeIpType(pParaDa, ParaLen) == 0) ||
				(get_valid_domain_name((char *)pParaDa, ParaLen) == 0))
			{
				memset(major_info.IP2,0,sizeof(major_info.IP2));
				memcpy(major_info.IP2,pParaDa, ParaLen);
				major_info.IP2Len = ParaLen;
				rebootflag = 2;
			}
			else
			{
				ret = 1;
				break;
			}
			#if 1
			gaoz_log_debug_printf(INFOR_MODE,"800c","major_info.IP2=%s,%d",major_info.IP2,major_info.IP2Len);
			#endif
		}
		else if(paraID == 0x0018)
		{
				if (ParaLen > 4)
				{
					ret = 1;
					break;
				}
				Int_To_ASCII_p(hex4CombineOne(pParaDa), pTmpDa, &uint8_tTmpDa);
				gaoz_log_debug_printf(INFOR_MODE,"800c","Int_To_ASCII_p=%s,%d\r\n",pTmpDa,uint8_tTmpDa);
				if (uint8_tTmpDa > 5)
				{
					ret = 1;
					break;
				}
				memcpy(major_info.Port1, pTmpDa, uint8_tTmpDa);
				major_info.Port1Len = uint8_tTmpDa;
				memcpy(major_info.Port2, pTmpDa, uint8_tTmpDa);
				major_info.Port2Len = uint8_tTmpDa;
				rebootflag = 2;
				#if 1
				gaoz_log_debug_printf(INFOR_MODE,"800c","major_info.Port1=%d,Port2=%d\r\n",hex4CombineOne((uint8_t *)major_info.Port1),hex4CombineOne((uint8_t *)major_info.Port2));
				#endif				
		}
		else if(paraID == 0x0027)
		{
				if (ParaLen != 4)
				{
					ret = 1;
					break;
				}
				memcpy(current_info.idleReportMaxTime, pParaDa, ParaLen);
				#if 1
				gaoz_log_debug_printf(INFOR_MODE,"800c","current_info.idleReportMaxTime=%d",hex4CombineOne(current_info.idleReportMaxTime));
				#endif
		}
		else if(paraID == 0x0029)
		{
				if (ParaLen != 4)
				{
					ret = 1;
					break;
				}
				memcpy(current_info.workReportMaxTime, pParaDa, ParaLen);
				#if 1
				gaoz_log_debug_printf(INFOR_MODE,"800c","current_info.workReportMaxTime=%d",hex4CombineOne(current_info.workReportMaxTime));
				#endif
		}
		else if(paraID == 0x002C)
		{
				if (ParaLen != 4)
				{
					ret = 1;
					break;
				}
				memcpy(current_info.idleReportMaxMeter, pParaDa, ParaLen);
				#if 1
				gaoz_log_debug_printf(INFOR_MODE,"800c","current_info.idleReportMaxMeter=%d",hex4CombineOne(current_info.idleReportMaxMeter));
				#endif
		}
		else if(paraID == 0x002E)
		{
				if (ParaLen != 4)
				{
					ret = 1;
					break;
				}
				memcpy(current_info.workReportMaxMeter, pParaDa, ParaLen);
				#if 1
				gaoz_log_debug_printf(INFOR_MODE,"800c","current_info.workReportMaxMeter=%d",hex4CombineOne(current_info.workReportMaxMeter));
				#endif
		}
		else if(paraID == 0x0030)
		{
			
		}
		else if(paraID == 0x0031)
		{
			
		}
		else if(paraID == 0x0032)
		{
			
		}
		else if(paraID == 0x0033)
		{
			
		}
		else if(paraID == 0x0034)
		{
			
		}
		else if(paraID == 0x0035)
		{
			
		}
		else if(paraID == 0x00FF0105)
		{
			if (ParaLen > 4)
			{
				ret = 1;
				break;
			}
			ulTmpDa = hex4CombineOne(pParaDa);//单位百米
			gaoz_gnss_mileage_set(ulTmpDa*100);
		}
 	
		else if(paraID == 0x00FF0108)
		{
			memset(vinbuf,0,18);
			memcpy(vinbuf,pParaDa,17);//copy vin

			if( vin_data_verification( (char *)vinbuf ) == pdPASS )
			{ 
				if(vehicle_set_vin(vinbuf))
				{
					gaoz_log_debug_printf(INFOR_MODE,"800b","set VIN mangpatio set pdpass vin:%s\r\n",vinbuf);
					ret = 0;
				} 
				else 
				{
					gaoz_log_debug_printf(INFOR_MODE,"800b","set VIN mangpatio set pdfail vin:%s\r\n",vinbuf);
					ret = 1;
				}				
			} 
			else 
			{
				gaoz_log_debug_printf(INFOR_MODE,"800b","set VIN mangpatio verification pdfail vin:%s\r\n",vinbuf);
				ret = 1;//设置失败
				break;
			} 
		} 
		else if(paraID == 0x00FF0902)
		{
			if(get_valid_domain_name((char *)pParaDa, ParaLen) == 0)
			{
				memset(current_info.IP1,0,sizeof(current_info.IP1));
				memcpy(current_info.IP1, pParaDa, ParaLen);
				current_info.IP1Len = ParaLen;
				rebootflag = 1;
			}
			else
			{
				ret = 1;
				break;
			}
			#if 1
			gaoz_log_debug_printf(INFOR_MODE,"800c","current_info.IP1(domain)=%s,%d",current_info.IP1,current_info.IP1Len);
			#endif
		}
		else if(paraID == 0x00FF0903)
		{
			if (ParaLen > 4)
			{
				ret = 1;
				break;
			}
			Int_To_ASCII_p(hex4CombineOne(pParaDa), pTmpDa, &uint8_tTmpDa);
			gaoz_log_debug_printf(INFOR_MODE,"800c","Int_To_ASCII_p=%s,%d\r\n",pTmpDa,uint8_tTmpDa);
			if (uint8_tTmpDa > 5)
			{
				ret = 1;
				break;
			}
			memcpy(current_info.Port1, pTmpDa, uint8_tTmpDa);
			current_info.Port1Len = uint8_tTmpDa;
			memcpy(current_info.Port2, pTmpDa, uint8_tTmpDa);
			current_info.Port2Len = uint8_tTmpDa;
			rebootflag = 1;
			#if 1
			gaoz_log_debug_printf(INFOR_MODE,"800c","domain.Port1=%d,Port2=%d\r\n",hex4CombineOne((uint8_t *)current_info.Port1),hex4CombineOne((uint8_t *)current_info.Port2));
			#endif	
		}
		else if(paraID == 0x00FF0904)
		{
			if (ParaLen != 1)
			{
				ret = 1;
				break;
			}
			if((pParaDa[0] == 0x01) || (pParaDa[0] == 0x10))
			{
				if(current_info.Mode != 0x10)
				{
					rebootflag = 3;
				}
				current_info.Mode = pParaDa[0];
			}
			else if((pParaDa[0] == 0x02) || (pParaDa[0] == 0x20))
			{
				major_info.Mode = pParaDa[0];
				rebootflag = 2;
			}
		}
		else
		{
			ret = 1;
			#if 1
			gaoz_log_debug_printf(INFOR_MODE,"800c",">>no paraID<<");
			#endif			
		}
		pParaDa += ParaLen; //指针指到下一个开始的位置		
	}
	if(ret)
	{
		gaoz_fibo_free(temp);
		return ret;
	}

	check = get_chk_xor((uint8_t*)(&current_info), sizeof(mangpatio_info_t)-1);
	current_info.Check = check;
	gaoz_log_debug_printf(INFOR_MODE,"800c","9529 <--%s,%d\r\n",(&current_info.Port1),sizeof(mangpatio_info_t));
	memcpy(temp,terid,4);	
	memcpy(&temp[4],"EDTT",4);
	memcpy(&temp[8],&current_info,sizeof(mangpatio_info_t));
	ret = file_termpara_write(MANGPATIOR_FILE,temp,sizeof(mangpatio_info_t)+8);
	if(ret < 0)
	{
		gaoz_fibo_free(temp);	
		return 1;
	}	

	/*
	ret = flash_spi_write_data(SF_SYS_PARA_FLAG_ADDR,(uint8_t *)temp,sizeof(terminal_info_t)+8);
	if(ret == pdPASS)
	{
		gaoz_log_debug_printf(INFOR_MODE,"800c",">>SF_SYS_PARA_FLAG_ADDR Okay<<");
	}
	ret = flash_spi_write_data(SF_SYS_PARA_BACKUP_ADDR,(uint8_t *)temp,sizeof(terminal_info_t)+8);
	if(ret == pdPASS)
	{
		gaoz_log_debug_printf(INFOR_MODE,"800c",">>SF_SYS_PARA_FLAG_ADDR Okay<<");
	}*/

	memcpy(&terinf,&current_info,sizeof(mangpatio_info_t));
	if(rebootflag == 1)
	{
		rebootflag = 0;
		/* 重启设备 */
		mangpatio_rst = NET_RST_START;//同步切换
	}
	else if(rebootflag == 2)
	{
		ret = savemajorip(&major_info,2);
		if(ret != 0)
		{
			gaoz_log_debug_printf(INFOR_MODE,"800c",">>savemajorip fail<<");
		}
	}
	else if(rebootflag == 3)
	{
		mangpatio_rst = NET_RST_END;//禁用
	}
	ret = 0;
	gaoz_fibo_free(temp);
	return ret;
}
/** ****************************************************************************
* @remarks      uint32_t mangpatio_erase_qxwzkey(void)
* @brief         擦除key
* @param[in]     无
* @param[out]    无
* @return        pdPASS成功 ，  pdFAIL失败
* @attention     无
*******************************************************************************/
static uint32_t mangpatio_erase_qxwzkey(void)
{
	uint32_t ret = pdFAIL;
	qxwz_save_cfg_infor_t qxwz_key_t = {0};

	int fd = fibo_file_open(QXWZKEY,FS_O_RDONLY);
	if(fd < 0)
	{
		gaoz_log_debug_printf(INFOR_MODE,"800c","fibo_file_open(QXWZKEY fail!!");
		ret = pdFAIL;
	}
	else
	{
		int rfd = fibo_file_close(fd);
		if(rfd < 0)
		{
			gaoz_log_debug_printf(INFOR_MODE,"800c","fibo_file_close fail!!");
			ret = pdFAIL;
		} 
		else
		{
			rfd = fibo_file_delete(QXWZKEY);
			if(rfd < 0)
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","fibo_file_delete QXWZKEY fail!!");
				ret = pdFAIL;
			} 
			else
			{
				memset(qxwz_key_t.app_key,0,sizeof(qxwz_key_t.app_key));
				memset(qxwz_key_t.app_secret,0,sizeof(qxwz_key_t.app_secret));
				HexToAsciiString((char *)qxwz_key_t.device_id,(char *)terid, 4);		
				sprintf((char *)qxwz_key_t.device_type, "%s", QXWZDEVICE_TYPE);
				ret = gaoz_qxwz_inject_cfg_save_flash_write(&qxwz_key_t);
			}
		}
	}

	return ret;
}
/** ****************************************************************************
* @remarks       static void parsepatio_protocol(uint8_t * temp, uint16_t len)
* @brief         协议解析 
* @param[in]     * temp缓存区，len长度
* @param[out]    无
* @return        无
* @attention     
*******************************************************************************/
static void parsepatio_protocol(uint8_t * temp, uint16_t len)
{
 	uint8_t reDa = 0;
	int ret = -1;
	national_net_res_msg_t patiores_msg;
	national808_net_devres_msg_t devres_msg;
	national808_ctrllock_msg_t  ctlock_msg;
	qxwz_save_cfg_infor_t qxwz_key_t = {0};
	//报文解析使用,文件头
	uint16_t Message_ID;
	uint16_t Message_Property;
	uint8_t Terminal_ID[6];
	uint16_t Message_Serial_Number;
	uint8_t *pMsgBody;

	uint16_t getSerialNum;
	uint16_t getID;
	uint8_t getResult;
	//u8 CryptFlag;
	u16 bodyLen;
	uint8_t secret_key[32] = { 0 };

 	uint8_t *tmpDa = (uint8_t *)gaoz_fibo_malloc(1024*2);
    if (tmpDa == NULL)
    {
		return;
    }
	if (len < 1024)
	{
		memcpy(tmpDa, temp, len);
		{	
			#if 1
			uint8_t debug_buf[1024] = {0};
			uint16_t debug_buf_len = 0;
			memset(debug_buf,0,sizeof(debug_buf));
            debug_buf_len = HexToAsciiString((char *)debug_buf,(char *)tmpDa, len);
			debug_buf[debug_buf_len] = '\r';
            debug_buf_len++;
            debug_buf[debug_buf_len] = '\n';
            debug_buf_len++;
			gaoz_log_debug_printf(INFOR_MODE,"800c", "parsepatio_protocol:%s,%d\r\n",debug_buf, debug_buf_len );
			#endif
		}
	}
	else
	{
		gaoz_fibo_free(tmpDa);
		return;
	}	
	
	Message_ID = (tmpDa[0]<<8)+tmpDa[1];					//消息ID
	if( Message_ID == 0x5001 )
	{
		mangpatio_decrypt_package_shell( (uint8_t *)tmpDa, (uint16_t *)&len );
		if (len == 0)
		{
			return;
		}
		Message_ID = (tmpDa[0] << 8) + tmpDa[1];					//消息ID
		Message_Property = ((tmpDa[2] << 8) + tmpDa[3]);	//消息体属性
		memcpy(Terminal_ID, &tmpDa[4], 6);
		Message_Serial_Number = (tmpDa[10] << 8) + tmpDa[11];		//下发流水账号码
		//pMsgBody = &tmpDa[12];								//消息体
		//消息体
		if ((Message_Property & 0x2000) == 0x2000)
		{
			pMsgBody = &tmpDa[16];
		}
		else
		{
			pMsgBody = &tmpDa[12];
		}
		Message_Property &= 0x3FF;
		bodyLen = Message_Property;
		//CryptFlag = (tmpDa[2] >> 2) & 0x07;
		//if (CryptFlag != 0x00)
		//{
		mangpatio_decrypt_package((uint8_t *)pMsgBody, (uint16_t *)&bodyLen, Message_Serial_Number);
		//}
	}
	else
	{
		Message_Property = ((tmpDa[2] << 8) + tmpDa[3]);	//消息体属性
		memcpy(Terminal_ID, &tmpDa[4], 6);
		Message_Serial_Number = (tmpDa[10] << 8) + tmpDa[11];		//下发流水账号码
		//pMsgBody = &tmpDa[12];								//消息体
		//消息体
		if ((Message_Property & 0x2000) == 0x2000)
		{
			pMsgBody = &tmpDa[16];
		}
		else
		{
			pMsgBody = &tmpDa[12];
		}
		Message_Property &= 0x3FF;
		bodyLen = Message_Property;
		// CryptFlag = (tmpDa[2] >> 2) & 0x07;
		// if (CryptFlag != 0x00)
		// {
		// 	mangpatio_decrypt_package(pMsgBody, &bodyLen, Message_Serial_Number);
		// }
	}
	switch (Message_ID)
	{
		case 0x8100:
		{
			switch(pMsgBody[2])
			{
				case 0:
					gaoz_log_debug_printf(INFOR_MODE,"800c","welcome");
					//先解密，之后再存储，这个是socket通信秘钥
					memcpy(secret_key, Terminal_ID, 6);
					secret_key[6] = (u8)(Message_Serial_Number >> 8);
					secret_key[7] = (u8)(Message_Serial_Number);
					strReverse(secret_key, &secret_key[8], 8);
					aes_decrypt_multi(&pMsgBody[7], 16, secret_key);
					aes_decrypt_multi(&pMsgBody[23], 16, secret_key);
					ret = mangpatio_saveserviceloadpara(&pMsgBody[3],1,NULL);
					patiores_msg.commane = TER_RESPONSE_REG;
					patiores_msg.results = ret;
					ret = mangpatio_net_patiores_queue_put(&patiores_msg);
					if(ret < 1)
					{
						gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 8100 fail!\r\n");
					}
					break;
				case 1:
					gaoz_log_debug_printf(INFOR_MODE,"800c","car reg");
					break;
				case 2:
					gaoz_log_debug_printf(INFOR_MODE,"800c","no car");
					break;
				case 3:
					gaoz_log_debug_printf(INFOR_MODE,"800c","ter reg");
					break;
				case 4:
					gaoz_log_debug_printf(INFOR_MODE,"800c","no ter");
					break;
				case 5:
					gaoz_log_debug_printf(INFOR_MODE,"800c","no sim");
					break;
				default:
					gaoz_log_debug_printf(INFOR_MODE,"800c","error");
					break;
			}
		} 
		break;
		case 0x8107:
		{
			patiores_msg.commane = TER_QUERY_SYS_VERSION;
			patiores_msg.results = pdPASS;
			ret = mangpatio_net_patiores_queue_put(&patiores_msg);
			if(ret < 1)
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 8107 fail!\r\n");
			}
		}
		break;
		case 0x9501:
		{
			gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_pu 9501 Body[4] == %d\r\n",pMsgBody[4]);
			switch(pMsgBody[4])
			{
				case 0:
					mangpatio_auth_reason_clear();
				case 1:
				{
					patiores_msg.commane = TER_RESPONSE_AUTH;
					patiores_msg.results = pdPASS;
					ret = mangpatio_net_patiores_queue_put(&patiores_msg);
					if(ret < 1)
					{
						gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9501 fail!\r\n");
					}
				}
				break;
				case 3:
				case 4:
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","9501 error");
					patiores_msg.commane = TER_RESPONSE_AUTH;
					patiores_msg.results = pdFAIL;
					ret = mangpatio_net_patiores_queue_put(&patiores_msg);
					if(ret < 1)
					{
						gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9501 fail!\r\n");
					}
				}
				break;
				default:
				break;
			}
			mangpatio_auth_recv_st = 0;
			mangpatio_auth_fail_times = 0;
		}
		break;
		case 0x9520:
		{
			getSerialNum = char2u16(&pMsgBody[0]);
			getID = char2u16(&pMsgBody[2]);
			getResult = pMsgBody[4];
			//gaoz_log_debug_printf(INFOR_MODE,"800c","getSerialNum[%x] getID[%x] getResult[%d]!\r\n",getSerialNum,getID,getResult);
			if(getID == 0x0506)
			{
				patiores_msg.commane = TER_RESPONSE_HEART ;
				patiores_msg.results = pdPASS;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 0506 fail!\r\n");
				}
				gaoz_log_debug_printf(INFOR_MODE,"800c","getSerialNum[%x] getID[%x] getResult[%d]!\r\n",getSerialNum,getID,getResult);
			}
			else if(getID == 0x0003)
			{
				mangpatio_saveserviceloadpara(NULL,2,NULL);	//zhuxiao
			}
			else if(getID == 0x0504)
			{
				patiores_msg.commane = TER_REALTIME_DATA_REQ;
				if(getResult == 0)
				{
					patiores_msg.results = pdPASS;
				}
				else
				{
					patiores_msg.results = pdFAIL;
				}
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 0504 fail!\r\n");
				}
				else
				{
					//gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 0504 ok!\r\n");
				}
			}
		}
		break;
		case 0x9529:
		{
			gaoz_log_debug_printf(INFOR_MODE,"800c","pMsgBody Message_Property <-,%d", Message_Property);			
			if(pMsgBody[0] >= 1)
			{
				reDa = mangpatio_setParafromservice(pMsgBody,Message_Property);
				patiores_msg.commane = TER_SET_SYS_PARRMETER;
				patiores_msg.results = pdPASS;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9529 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = reDa;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_put 9529 fail!\r\n");
				}	
			}
			else
			{
				patiores_msg.commane = TER_SET_SYS_PARRMETER;
				patiores_msg.results = pdFAIL;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9529 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 1;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9529 fail!\r\n");
				}
			}
		}
		break;
		case 0x9560:
		{
			patiores_msg.commane = TER_QUERY_REALTIME;
			patiores_msg.results = pdPASS;
			ret = mangpatio_net_patiores_queue_put(&patiores_msg);
			if(ret < 1)
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9560 fail!\r\n");
			}
			
		}
		break;
		case 0x9562:
		{	
			if( (mangpatio_ctrllock_state_get() == CTLOCK_INIT) ||
				(mangpatio_ctrllock_state_get() == CTLOCK_OK))
			{
				patiores_msg.commane = TER_CONTROL_TER_FLASHING_HORNS;
				patiores_msg.results = pdPASS;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9562 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 0;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_put 9562 fail!\r\n");
				}
	
				if(pMsgBody[0] != 0)
				{
					mangpatio_ctrllock_state_set(CTLOCK_PATIO_SPK);
				}
				if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)	
				{
					ctlock_msg.lock_type = pMsgBody[0]|MANGPATIO_CTLOCK_APPEND;
					ctlock_msg.lock_res.respondcmd = Message_ID;
					ctlock_msg.lock_res.respondsn = Message_Serial_Number;
					ctlock_msg.lock_res.respondsta = 0; 
					ctlock_msg.lock_err = 0;
					ret = mangpatio_ctlock_queue_put(&ctlock_msg);
					if(ret < 1)
					{
						gaoz_log_debug_printf(INFOR_MODE,"800c","ctlock_queue_put 9562 fail!\r\n");
					}
				}
				else
				{
					mangpatio_ctrllock_state_set( CTLOCK_OK);
				}
			}
			else
			{
				patiores_msg.commane = TER_CONTROL_TER_FLASHING_HORNS;
				patiores_msg.results = pdPASS;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9562 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 0;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_put 9562 fail!\r\n");
				}	
			}
		}
		break;	
		case 0x9563:
		{
			if( (mangpatio_ctrllock_state_get() == CTLOCK_INIT) ||
				(mangpatio_ctrllock_state_get() == CTLOCK_OK))
			{
				patiores_msg.commane = TER_CONTROL_TERMINAL_ACK;
				patiores_msg.results = pdPASS;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9563 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 0;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{	
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9563 fail!\r\n");
				}
				if((pMsgBody[0] == 1) || (pMsgBody[0] == 7))
				{
					mangpatio_ctrllock_state_set(CTLOCK_PATIO_UNLOCK);
					if(pMsgBody[0] == 7)
					{
						setrelay_sta(1);
					}
				}
				else
				{
					mangpatio_ctrllock_state_set(CTLOCK_PATIO_LOCK);
					if(pMsgBody[0] == 8)
					{
						setrelay_sta(2);
					}
				}
				if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)	
				{
					ctlock_msg.lock_type = pMsgBody[0]|MANGPATIO_CTLOCK_APPEND;
					ctlock_msg.lock_res.respondcmd = Message_ID;
					ctlock_msg.lock_res.respondsn = Message_Serial_Number;
					ctlock_msg.lock_res.respondsta = 0; 
					ctlock_msg.lock_err = 0;
					ret = mangpatio_ctlock_queue_put(&ctlock_msg);
					if(ret < 1)
					{
						gaoz_log_debug_printf(INFOR_MODE,"800c","ctlock_queue_put 9563 fail!\r\n");
					}
				}
				else
				{
					mangpatio_ctrllock_state_set( CTLOCK_OK);
				}
			}
			else
			{
				patiores_msg.commane = TER_CONTROL_TERMINAL_ACK;
				patiores_msg.results = pdFAIL;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9563 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 1;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9563 fail!\r\n");
				}
			}
		}
		break;
		case 0x9565:
		{
			if(setrelay_sta(pMsgBody[0]) == pdPASS)
			{
				patiores_msg.commane = TER_CONTROL_TER_POWER;
				patiores_msg.results = pdPASS;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9565 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 0;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9565 fail!\r\n");
				}
			}
			else
			{
				patiores_msg.commane = TER_CONTROL_TER_POWER;
				patiores_msg.results = pdFAIL;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9565 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 1;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9565 fail!\r\n");
				}
			}
		}	
		break;
		
		case 0x9108:
		{
			if(cjson_parseurl((char *)pMsgBody,Message_Property,URL_MAJOR) == 1)
			{
				patiores_msg.commane = TER_CONTROL_TER_FOTA;
				patiores_msg.results = pdPASS;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800b","mangpatio_net_patiores_queue_put 9108 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 0;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800b","mangpatio_net_devres_queue_put 9108 fail!\r\n");
				}
			}
			else
			{
				patiores_msg.commane = TER_CONTROL_TER_FOTA;
				patiores_msg.results = pdPASS;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800b","mangpatio_net_patiores_queue_put 9108 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 1;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800b","mangpatio_net_devres_queue_put 9108 fail!\r\n");
				}
			}
		}
		break;
		case 0x9571:
		{
			#if 0
			if ((Message_Property >= 6)&&(chk_valid_bcd_num(pMsgBody, 6) == 1))
			{
					memcpy(currinf.ManageID,terinf.ManageID,6);
					int ret = mangpatio_saveserviceloadpara(pMsgBody,9,NULL);	
					if(ret < 1)
					{
						gaoz_log_debug_printf(INFOR_MODE,"800c","9571 saveserviceloadpara error");
						patiores_msg.commane = TER_CONTROL_TER_UPDATE_ID;
						patiores_msg.results = pdFAIL;
						ret = mangpatio_net_patiores_queue_put(&patiores_msg);
						if(ret < 1)
						{
							gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9571 fail!\r\n");
						}
						devres_msg.respondcmd = Message_ID;
						devres_msg.respondsn = Message_Serial_Number;
						devres_msg.respondsta = 0;
						ret = mangpatio_net_devres_queue_put(&devres_msg);
						if(ret < 1)
						{
							gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9571 fail!\r\n");
						}
					}
					else
					{
						/*restart authoration*/
						patiores_msg.commane = TER_CONTROL_TER_UPDATE_ID;
						patiores_msg.results = pdPASS;
						ret = mangpatio_net_patiores_queue_put(&patiores_msg);
						if(ret < 1)
						{
							gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9571 fail!\r\n");
						}
						devres_msg.respondcmd = Message_ID;
						devres_msg.respondsn = Message_Serial_Number;
						devres_msg.respondsta = 0;
						ret = mangpatio_net_devres_queue_put(&devres_msg);
						if(ret < 1)
						{
							gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9571 fail!\r\n");
						}
						
					}
			}
			else
			{
				patiores_msg.commane = TER_CONTROL_TER_UPDATE_ID;
				patiores_msg.results = pdFAIL;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9571 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 0;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9571 fail!\r\n");
				}
			}
			#endif		
		}
		break;
		case 0x9574:
		{
			if(pMsgBody[1] == 0x04) //重启
			{
				reset_reason_set(EVENT_RESET_REASON_SERVER_REQUIRE);
				gaoz_sys_abnormal_reset_set( SYS_RESET_APP );
			}
			else if(pMsgBody[1] == 0x07)//重新定位
			{
				gaoz_event_input_data_set( EVENT_VEHICLE , 2);
			}
		}
		break;
		case 0x9575:
		case 0x9576:
		{
			patiores_msg.commane = TER_CONTROL_TER_RESET;
			patiores_msg.results = pdPASS;
			ret = mangpatio_net_patiores_queue_put(&patiores_msg);
			if(ret < 1)
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9575 fail!\r\n");
			}
			devres_msg.respondcmd = Message_ID;
			devres_msg.respondsn = Message_Serial_Number;
			devres_msg.respondsta = 0;
			ret = mangpatio_net_devres_queue_put(&devres_msg);
			if(ret < 1)
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9575 fail!\r\n");
			}
			ret = n808_cle_save_data();
			if(ret != pdPASS)
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","gaoz_save_data_open fail[%d]\n", sfb_handle_id);
			}
			else
			{
				memset(mangpatio_data_st.send_data,0,UNIT_FLASH_DATA_LEN);
				mangpatio_data_st.send_datalen = 0;
				//mangpatio_data_st.send_st = N808_SENDRSP_OK;
			}
		}
		break;
		case 0x9579:
		{
			uint8_t rsaDa[1024]={0};
			memcpy(rsaDa,pMsgBody,Message_Property);	
			rsaDa[Message_Property] = get_chk_xor(rsaDa,Message_Property);
			ret = file_termpara_write(RSAPARA,(INT8 *)rsaDa,Message_Property+1);
			if(ret < 0)
			{
				patiores_msg.commane = TER_CONTROL_TER_BT_UPDATESE;
				patiores_msg.results = pdFAIL;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9579 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 1;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9579 fail!\r\n");
				}
			}
			else
			{
				patiores_msg.commane = TER_CONTROL_TER_BT_UPDATESE;
				patiores_msg.results = pdPASS;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9579 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 0;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9579 fail!\r\n");
				}
				//if(memcmp(&pMsgBody[hex2CombineOne(pMsgBody)+2+1],bt_getname(),sizeof(bt_getname())) == NULL)
				memset(rsaDa,0,sizeof(rsaDa));
				ret = file_termpara_read(RSAPARA,(INT8 *)rsaDa);
				if(ret == 0)
				{
					bluetooth_setrsa(&rsaDa[2],hex2CombineOne(rsaDa));
					if(( memcmp( ( uint8_t *)bluetooth_getname(), ( uint8_t *)&rsaDa[hex2CombineOne(rsaDa)+2+1] ,rsaDa[hex2CombineOne(rsaDa)+2] ) != 0 )
						|| ( strlen((char *)bluetooth_getname() ) != rsaDa[hex2CombineOne(rsaDa)+2] ))
					{
						bluetooth_setname(&rsaDa[hex2CombineOne(rsaDa)+2+1],rsaDa[hex2CombineOne(rsaDa)+2]);
						gaoz_log_debug_printf(INFOR_MODE,"800c","bt_getname()= %s,%d", bluetooth_getname(),strlen((char *)bluetooth_getname()));
						/*ret = gaoz_ble_drive_data_close( (char *)bt_getname());
						if(ret == pdPASS)
						{
							gaoz_log_debug_printf(INFOR_MODE,"800c","gaoz_ble_drive_data_close  OKay!\r\n");
						}*/
						ret = gaoz_ble_drive_data_open( (char *)bluetooth_getname(),bluetooth_recv_state_cb );
						if( ret == pdPASS )
						{
							gaoz_log_debug_printf(INFOR_MODE,"800c","gaoz_ble_drive_data_open Okay! %s\r\n", bluetooth_getname());
						}
					}
					else
					{
						gaoz_log_debug_printf(INFOR_MODE,"800c","ble name same = %s,%d", bluetooth_getname(),strlen((char *)bluetooth_getname()));
					}
				}
			}			
		}
		break;
		case 0x9581:
		{
			patiores_msg.commane = TER_QUERY_RSA;
			patiores_msg.results = pdPASS;
			ret = mangpatio_net_patiores_queue_put(&patiores_msg);
			if(ret < 1)
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9581 fail!\r\n");
			}
			devres_msg.respondcmd = Message_ID;
			devres_msg.respondsn = Message_Serial_Number;
			devres_msg.respondsta = 0;
			ret = mangpatio_net_devres_queue_put(&devres_msg);
			if(ret < 1)
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9581 fail!\r\n");
			}
		}
		break;
		case 0x9582:
		{
			
		}
		break;
		case 0x9590:
		{
			patiores_msg.commane = TER_QUERY_SYS_PARRMETER;
			patiores_msg.results = pdPASS;
			ret = mangpatio_net_patiores_queue_put(&patiores_msg);
			if(ret < 1)
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_res_queue_put 9590 fail!\r\n");
			}
			devres_msg.respondcmd = Message_ID;
			devres_msg.respondsn = Message_Serial_Number;
			devres_msg.respondsta = 0;
			ret = mangpatio_net_devres_queue_put(&devres_msg);
			if(ret < 1)
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_put 9590 fail!\r\n");
			}
		}
		break;
		case 0x9594:
		{
			
		}
		break;
		case 0x9635: /*qxwz*/
		{
			uint8_t appkey[100]={0};
			uint8_t appkey_index = 0;
			if((Message_Property == 0) || (Message_Property > 100))
			{
				break;	
			}
			memcpy(appkey,pMsgBody,Message_Property);	
			appkey[Message_Property] = get_chk_xor(appkey,Message_Property);
			ret = file_termpara_write(QXWZKEY,(INT8 *)appkey,Message_Property+1);
			if(ret < 0)
			{
				patiores_msg.commane = TER_CONTROL_TER_QXWZKEY_SET;
				patiores_msg.results = pdFAIL;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9635 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 1;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_put 9635 fail!\r\n");
				}
			}
			else
			{
				appkey_index = 0;
				if((appkey[appkey_index] < QXWZ_CFG_MAX_LEN) && (appkey[appkey_index] != 0))
				{
					appkey_index ++;
					memcpy(qxwz_key_t.app_key,&appkey[appkey_index],appkey[appkey_index-1]);
				}
				gaoz_log_debug_printf(INFOR_MODE,"800c","app_key %s\r\n",qxwz_key_t.app_key);
				appkey_index += appkey[appkey_index-1];
				if((appkey[appkey_index] < QXWZ_CFG_MAX_LEN) && (appkey[appkey_index] != 0))
				{
					appkey_index ++;
					memcpy(qxwz_key_t.app_secret,&appkey[appkey_index],appkey[appkey_index-1]);
				}
				gaoz_log_debug_printf(INFOR_MODE,"800c","app_secret %s\r\n",qxwz_key_t.app_secret);
				HexToAsciiString((char *)qxwz_key_t.device_id,(char *)terid, 4);
				gaoz_log_debug_printf(INFOR_MODE,"800c","device_id %s\r\n",qxwz_key_t.device_id);
				sprintf((char *)qxwz_key_t.device_type, "%s", QXWZDEVICE_TYPE);
				gaoz_log_debug_printf(INFOR_MODE,"800c","device_type %s\r\n",qxwz_key_t.device_type);
				ret = gaoz_qxwz_inject_cfg_save_flash_write(&qxwz_key_t);
				if(ret == pdPASS)
				{
					patiores_msg.commane = TER_CONTROL_TER_QXWZKEY_SET;
					patiores_msg.results = pdPASS;
					ret = mangpatio_net_patiores_queue_put(&patiores_msg);
					if(ret < 1)
					{
						gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9635 fail!\r\n");
					}
					devres_msg.respondcmd = Message_ID;
					devres_msg.respondsn = Message_Serial_Number;
					devres_msg.respondsta = 0;
					ret = mangpatio_net_devres_queue_put(&devres_msg);
					if(ret < 1)
					{
						gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_put 9635 fail!\r\n");
					}
				}
				else
				{
					patiores_msg.commane = TER_CONTROL_TER_QXWZKEY_SET;
					patiores_msg.results = pdFAIL;
					ret = mangpatio_net_patiores_queue_put(&patiores_msg);
					if(ret < 1)
					{
						gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9635 fail!\r\n");
					}
					devres_msg.respondcmd = Message_ID;
					devres_msg.respondsn = Message_Serial_Number;
					devres_msg.respondsta = 1;
					ret = mangpatio_net_devres_queue_put(&devres_msg);
					if(ret < 1)
					{
						gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_put 9635 fail!\r\n");
					}
				}
			}	
		}
		break;
		case 0x9636:
		{
			patiores_msg.commane = TER_QUERY_QXWZPARA;
			patiores_msg.results = pdPASS;
			ret = mangpatio_net_patiores_queue_put(&patiores_msg);
			if(ret < 1)
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9636 fail!\r\n");
			}
			devres_msg.respondcmd = Message_ID;
			devres_msg.respondsn = Message_Serial_Number;
			devres_msg.respondsta = 0;
			ret = mangpatio_net_devres_queue_put(&devres_msg);
			if(ret < 1)
			{
				gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_put 9636 fail!\r\n");
			}
		}
		break;
		case 0x9637:
		{
			ret = mangpatio_erase_qxwzkey();
			if(ret == pdPASS)
			{
				patiores_msg.commane = TER_CONTROL_TER_QXWZKEY_CLE;
				patiores_msg.results = pdPASS;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9637 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 0;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_put 9637 fail!\r\n");
				}
			}
			else
			{
				patiores_msg.commane = TER_CONTROL_TER_QXWZKEY_CLE;
				patiores_msg.results = pdPASS;
				ret = mangpatio_net_patiores_queue_put(&patiores_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_patiores_queue_put 9637 fail!\r\n");
				}
				devres_msg.respondcmd = Message_ID;
				devres_msg.respondsn = Message_Serial_Number;
				devres_msg.respondsta = 1;
				ret = mangpatio_net_devres_queue_put(&devres_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"800c","mangpatio_net_devres_queue_put 9637 fail!\r\n");
				}
			}
		}
		break;
		default:
            
        break;
    }
	gaoz_fibo_free(tmpDa);
}

/** ****************************************************************************
* @remarks       static uint32_t parse_mangpatio_recv_data( uint8_t *da, uint16_t len )
* @brief        解析平台下发数据
* @param[in]     *da缓存区，len长度
* @param[out]    无
* @return        pdPASS 数据发送成功 pdFAIL 数据发送失败
* @attention     
*******************************************************************************/
static uint32_t parse_mangpatio_recv_data( uint8_t *da, uint16_t len )
{
    uint32_t res = pdFAIL;    
	uint16_t CR_i;
	uint16_t tmpLen, RawDaLen;
	uint16_t CR_i_Start;
	uint16_t iStart, iEnd;	//记录当前包的开始和结束位置

	uint16_t i,j;

	uint8_t *GPRS_Response = da;
	uint16_t AllPac_Len;
	
    gaoz_mutex_lock(mangpatio_recv_data_mutex);
	AllPac_Len = len;
    uint8_t *temp = (uint8_t *)gaoz_fibo_malloc(1024*2);
    if (temp == NULL)
    {
        return res;
    }

	if (AllPac_Len == 0)
	{
		gaoz_fibo_free(temp);
		return res;
	}

	CR_i = 0;

	while (CR_i < AllPac_Len)
	{
		CR_i_Start = CR_i;

		if (mangpatio_recv_data.len != 0)	
		{
			//说明上次有半包情况，半包情况，在extr_da中，必然第一个是7E
			memcpy(temp, mangpatio_recv_data.data, mangpatio_recv_data.len);

			//开始找尾
			while (GPRS_Response[CR_i] != 0x7E)
			{
				CR_i ++;
				if (CR_i >= AllPac_Len)
				{
					//极端情况，整包数据，没有结尾的7e
					tmpLen = mangpatio_recv_data.len + CR_i - CR_i_Start;
					//先判断数组不要越界
					if (tmpLen > PATIO_RX_BUF_SIZE)
					{
						//假设收到了外太空信号，不处理
						gaoz_fibo_free(temp);
						return res;
					}

					memcpy(&mangpatio_recv_data.data[mangpatio_recv_data.len], &GPRS_Response[CR_i_Start], CR_i - CR_i_Start);
					mangpatio_recv_data.len = tmpLen;
					gaoz_fibo_free(temp);
					return res;
				}
			}

			//找到尾巴了，把这段数据存入temp数据，之后等待进行转义的处理
			if ( (mangpatio_recv_data.len + CR_i - CR_i_Start + 1) < PATIO_RX_BUF_SIZE )
			{
				memcpy(&temp[mangpatio_recv_data.len], &GPRS_Response[CR_i_Start], CR_i - CR_i_Start + 1);
				//temp中的数据，包括头尾
				RawDaLen = mangpatio_recv_data.len + CR_i - CR_i_Start + 1;
			}
			else
			{
				//temp中的数据，包括头尾
				RawDaLen = 0;
			}

			mangpatio_recv_data.len = 0;
			CR_i ++; //保证下次检查数据，是最新的
		}
		else
		{
			//上一包没有残余数据, 找头
			while (GPRS_Response[CR_i] != 0x7E)
			{
				CR_i ++;
				if (CR_i >= AllPac_Len)
				{
					//极端情况，一包数据，只收到了两个7e中间的 几个字节，暂时不考虑
					gaoz_fibo_free(temp);
					return res;
				}
			}
			iStart = CR_i; //找到了7E头
			//开始找尾
			CR_i ++;
			if (CR_i >= AllPac_Len)
			{
				tmpLen = CR_i - iStart;

				//先判断数组不要越界
				if (tmpLen > PATIO_RX_BUF_SIZE)
				{
					tmpLen = 0;
				}
				//属于残余数据，保存进extr_da
				memcpy(mangpatio_recv_data.data, &GPRS_Response[iStart], tmpLen);
				mangpatio_recv_data.len = tmpLen;
				gaoz_fibo_free(temp);
				return res;
			}

			while (GPRS_Response[CR_i] != 0x7E)
			{
				CR_i ++;
				if (CR_i >= AllPac_Len)
				{
					tmpLen = CR_i - iStart;

					//先判断数组不要越界
					if (tmpLen > PATIO_RX_BUF_SIZE)
					{
						tmpLen = 0;
					}
					//属于残余数据，保存进extr_da
					memcpy(mangpatio_recv_data.data, &GPRS_Response[iStart], tmpLen);
					mangpatio_recv_data.len = tmpLen;
					gaoz_fibo_free(temp);
					return res;
				}
			}
			iEnd = CR_i; //找到了7E end tag

			CR_i ++; //保证下次检查数据，是最新的

			//找到尾巴了，把这段数据存入temp数据，之后等待进行转义的处理
			if ( (iEnd - iStart + 1) < PATIO_RX_BUF_SIZE )
			{
				memcpy(temp, &GPRS_Response[iStart], iEnd - iStart + 1);
				//temp中的数据，包括头尾
				RawDaLen = iEnd - iStart + 1;
			}
			else
			{
				RawDaLen = 0;
			}			
		}

		if (RawDaLen == 0)
		{
			continue;
		}
		//转义
		//不再解析头尾
		RawDaLen --; 
		if (RawDaLen == 1)
		{
			//说明只收到了两个7E，有可能就是中间漏掉一包，为了防止
			//两个7E，我们再重新查找数据，最多丢一包
			CR_i --;
			continue;
		}
		for (i=1,j=0;i<RawDaLen;i++)
		{
			if ( (temp[i] == 0x7D) && (temp[i+1] == 0x02) )
			{
				temp[j++] = 0x7E;
				i++;
			}
			else if ( (temp[i] == 0x7D) && (temp[i+1] == 0x01) )
			{
				temp[j++] = 0x7D;
				i++;
			}
			else
			{
				temp[j++] = temp[i];
			}
		}
		
		// 计算校验，只有校验OK才可以解析
		if (get_chk_xor(temp, j) != 0)
		{		
			continue;
		}

		//把解析单独弄出去，清晰些
		parsepatio_protocol(temp, j);
        res = pdPASS;
	}
	gaoz_fibo_free(temp);
    gaoz_mutex_unlock( mangpatio_recv_data_mutex );
	//gaoz_log_debug_printf(INFOR_MODE,"800c", "<<<gaoz_mutex_unlock( mangpatio_recv_data_mutex>>>\r\n" );
    return res;
}


/** ****************************************************************************
* @remarks       static uint32_t mangpatio_decrypt_package( uint8_t * pDa, uint16_t * len, uint16_t serailNum )
* @brief         网络数据包解密
* @param[in]     *pDa 数据源地址，serailNum 序列号
* @param[out]    *len 返回的数据长度
* @return        pdPASS 成功，pdFAIL 失败
* @attention     无
*******************************************************************************/
static uint32_t mangpatio_decrypt_package( uint8_t * pDa, uint16_t *len, uint16_t serailNum )
{
	uint8_t tmpDa[16] = { 0 };

	//兼容没有使用加密机制的机器
	if ( terinf.rs[0] != 1 )
	{
		return pdFAIL;
	}
	
	memcpy(tmpDa, ( uint8_t * )&terinf.rs[1], 16);
	tmpDa[0] ^= (uint8_t)(serailNum >> 8);
	tmpDa[1] ^= (uint8_t)(serailNum);

	aes_decrypt_multi(pDa, *len, tmpDa);
	
	return pdPASS;
}

/** ****************************************************************************
* @remarks       void mangpatio_encrypt_package( uint8_t * pDa, uint16_t * len, uint16_t serailNum )
* @brief         网络数据包加密
* @param[in]     *pDa 数据源地址，serailNum 序列号
* @param[out]    *len 返回的数据长度
* @return        pdPASS 成功，pdFAIL 失败
* @attention     加密，只针对消息体部分 不足8字节倍数，补0x00，凑足8字节
*******************************************************************************/
#if( MANGPATIO_USER_AES_ENCRYPT == 1 )
static uint32_t mangpatio_encrypt_package( uint8_t *pDa, uint16_t *len, uint16_t serailNum )
{
	uint16_t startBodyIDX;
	uint16_t newBodyLen;
	uint16_t PacLen;
	uint8_t addLen;
	uint8_t pckMulSta;
	uint8_t tmpDa[16];
	
	//兼容没有使用加密机制的机器
	if (terinf.rs[0] != 1)
	{
		return pdFAIL;
	}
	
	PacLen = *len;

	//心跳类的没有消息体，长度=0，下面程序会报错
	if (PacLen <= 0x0d)
	{
		return pdFAIL;
	}

	//对分包项的检查
	pckMulSta = pDa[2] & 0x20;

	if (pckMulSta == 0)
	{
		startBodyIDX = 12;
	}
	else
	{
		startBodyIDX = 16;
	}
	if (PacLen < startBodyIDX)
	{
		return pdFAIL;
	}

	//让新的msg body len 补齐16的倍数
	newBodyLen = PacLen - startBodyIDX - 1;
	addLen = newBodyLen % 16;
	if (addLen != 0)
	{
		addLen = 16 - addLen;
	}
	memset(&pDa[startBodyIDX + newBodyLen], 0, addLen);
	newBodyLen += addLen;

	memcpy(tmpDa, ( uint8_t * )&terinf.rs[1], 16);

	tmpDa[0] ^= (uint8_t)(serailNum >> 8);
	tmpDa[1] ^= (uint8_t)(serailNum);
	
	aes_encrypt_multi(&pDa[startBodyIDX], newBodyLen, tmpDa);

	pDa[2] &= 0xF8;
	pDa[2] |= 0x08;
	pDa[2] |= (uint8_t)(newBodyLen >> 8) & 0x03;
	pDa[3] = (uint8_t)newBodyLen;

	PacLen += addLen;
	pDa[PacLen - 1] = get_chk_xor(pDa, PacLen - 1);
	(*len) = PacLen;

	return pdPASS;
}
#endif
/** ****************************************************************************
* @remarks       static uint32_t mangpatio_decrypt_package_shell( uint8_t* pDa, uint16_t* len )
* @brief         网络数据包外层数据包解密
* @param[in]     *pDa 数据源地址
* @param[out]    *len 返回的数据长度
* @return        pdPASS 成功，pdFAIL 失败
* @attention     无
*******************************************************************************/
static uint32_t mangpatio_decrypt_package_shell( uint8_t* pDa, uint16_t* len )
{
	#define  gloAESKey	"\x0C\xF5\x82\xFC\x24\x71\xDC\x13\x53\x8B\xEA\x2F\xB6\x85\xA6\xFD"
	#define  BJ_TIMESTAMP ( 946656000 ) /*2000-01-01 00:00:00 时间戳*/
	
	uint8_t tmpDa[16];
	uint8_t newData[1024 + 30];
	uint16_t i;
	//u16 idx;
	uint8_t tmpBit;
	uint16_t dataLen, dataInLen;
	uint16_t cmd;
	uint32_t timeStamp, timeStampL;
	
	dataInLen = *len;
	*len = 0;	//先假设数据是错误的
	/*if (get_chk_xor(pDa, dataInLen) != 0)
	{
		return;
	}*/
	//循环右移1位
	tmpDa[0] = pDa[2];
	tmpBit = 0x80 & (tmpDa[0]<<7);
	tmpDa[0] = 0x7F & (tmpDa[0] >> 1);
	tmpDa[0] |= tmpBit;
	//循环左移1位
	tmpDa[1] = pDa[3];
	tmpBit = 0x01 & (tmpDa[1] >> 7);
	tmpDa[1] = 0xFE & (tmpDa[1] << 1);
	tmpDa[1] |= tmpBit;
	//确定长度
	dataLen = (tmpDa[0] << 8) | tmpDa[1];
	dataLen -= 1;	//去掉校验部分

	if((dataLen > 1024+20) || (dataLen > (dataInLen-5)))
	{
		return pdFAIL;
	}

	//这样写，实际是为了防止秘钥直接明文暴露在程序中。
	memcpy(tmpDa, gloAESKey, 16);
	for (i = 0; i < 16;i++)
	{
		tmpDa[i] ^= 0x5A;
	}

	//开始解密
	memcpy(newData, &pDa[4], dataLen);
	aes_decrypt_multi(newData, dataLen, tmpDa);
	//先判断时间戳是否正确
	//关于通用应答的，不判断时间戳，鉴权应答的也不判断时间戳。
	timeStamp = (newData[0] << 24) | 
				(newData[1] << 16) |
				(newData[2] << 8) |
				newData[3];

	cmd = (newData[4] << 8) | newData[5];

	//除去通用应答、注册应答、鉴权应答不判断时间戳，其它都判断。
	if ((cmd != 0x8100) && (cmd != 0x9501))
	{
		gaoz_rtc_sys_get( RTC_TIM_TIMESTAMP, (uint32_t *)&timeStampL);
		timeStampL = timeStampL - BJ_TIMESTAMP;

		if (timeStamp > timeStampL)
		{
			if ((timeStamp - timeStampL) > 3600)
			{
				//时间戳错误，重新鉴权
				//lets_re_auth_after5sec();
				return pdFAIL;
			}
		}
		else
		{
			if ((timeStampL - timeStamp) > 3600)
			{
				//时间戳错误，重新鉴权
				//lets_re_auth_after5sec();
				return pdFAIL;
			}
		}
	}
	
	//程序走到这里说明，以上校验判断都是正确的，下面开始把内部的报文，写入新数组
	//-9： 两个字节的5001，两个字节的长度，4个字节的时间，1个字节的校验
	memcpy(pDa, &newData[4], dataInLen-9);	

	*len = dataInLen - 9;

	return pdPASS;
}

/** ****************************************************************************
* @remarks       static uint32_t mangpatio_encrypt_package_shell( uint8_t* pDa, uint16_t* len )
* @brief         网络数据包二次加密
* @param[in]     *pDa 数据源地址
* @param[out]    *len 返回的数据长度
* @return        pdPASS 成功，pdFAIL 失败
* @attention     加密，第二次加密 不足8字节倍数，补0x00，凑足8字节
*******************************************************************************/
#if( MANGPATIO_USER_AES_ENCRYPT == 1 )
static uint32_t mangpatio_encrypt_package_shell( uint8_t* pDa, uint16_t* len )
{
	#define  gloAESKey	"\x0C\xF5\x82\xFC\x24\x71\xDC\x13\x53\x8B\xEA\x2F\xB6\x85\xA6\xFD"
	#define  BJ_TIMESTAMP ( 946656000 ) /*2000-01-01 00:00:00 时间戳*/

	uint8_t addLen;
	//u8 pckMulSta;
	uint8_t tmpDa[16];
	uint8_t newData[1024+30];
	
	uint16_t startBodyIDX;
	uint16_t newBodyLen;
	uint16_t PacLen;
	uint16_t i;
	uint16_t idx;
	uint8_t tmpBit;
	uint32_t ulTmp;

	PacLen = *len;
	if ((PacLen > 1024) || (PacLen == 0))
	{
		return pdFAIL;
	}

	//写入特殊命令字
	memcpy(newData, "\x50\x01", 2);
	idx = 2;

	//让新的msg body len 补齐16的倍数
	newBodyLen = PacLen - 1 + 4;	//先去掉一个校验， 增加时间戳
	addLen = newBodyLen % 16;
	if (addLen != 0)
	{
		addLen = 16 - addLen;
	}

	//写入长度
	newBodyLen += addLen+1;
	if (newBodyLen > 1034)
	{
		return pdFAIL;
	}

	//循环左移1位
	newData[idx] = (u8)(newBodyLen>>8);
	tmpBit = 0x01&(newData[idx]>>7);
	newData[idx] = (newData[idx] << 1) & 0xFE;
	newData[idx] |= tmpBit;
	idx++;

	//循环右移1位
	newData[idx] = (u8)(newBodyLen);
	tmpBit = 0x80 & (newData[idx]<<7);
	newData[idx] = 0x7F & (newData[idx] >> 1);
	newData[idx] |= tmpBit;
	idx++;

	//时间戳
	startBodyIDX = idx;
	gaoz_rtc_sys_get( RTC_TIM_TIMESTAMP, (uint32_t *)&ulTmp);
	ulTmp = ulTmp - BJ_TIMESTAMP;

	newData[idx++] = (u8)(ulTmp>>24);
	newData[idx++] = (u8)(ulTmp >> 16);
	newData[idx++] = (u8)(ulTmp >> 8);
	newData[idx++] = (u8)(ulTmp);

	//把原来的消息头和消息体赋值入数组,
	memcpy(&newData[idx], pDa, PacLen-1);
	idx += PacLen-1;

	//简单方法，防止越界
	if (idx > 1040)
	{
		return pdFAIL;
	}

	//写入填充报文
	memset(&newData[idx], 0xA0, addLen);
	idx += addLen;

	//秘钥简单转换一下
	memcpy(tmpDa, gloAESKey, 16);
	for (i = 0; i < 16; i++)
	{
		tmpDa[i] ^= 0x5A;
	}

	//加密了
	aes_encrypt_multi(&newData[startBodyIDX], idx-startBodyIDX, tmpDa);

	//可以开始计算校验了
	newData[idx] = get_chk_xor(newData, idx);
	idx++;

	if (idx < 1054)
	{
		memcpy(pDa, newData, idx);
		(*len) = idx;
	}
	else
	{
		(*len) = 0;
	}

	return pdPASS;
}
#endif
/** ****************************************************************************
* @remarks       static void national_net_run_para_init( void )
* @brief         网络运行参数初始化
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     在系统启动后进行创建
*******************************************************************************/
static void mangpatio_net_run_para_init( void )
{
	mangpatio_broken_link = 0;
    mangpatio_runing_st = TER_RUNING_INIT;
    mangpatio_response = PLAT_NO_RESPONSE;
	mangpatio_ctlock = CTLOCK_INIT;
	mangpatio_sleep_wakeup_st = SLEEP_WAKEUP_INIT;
	mangpatio_data_st.send_data  = (uint8_t *)gaoz_fibo_malloc( UNIT_FLASH_DATA_LEN );
    if( mangpatio_data_st.send_data == NULL )
    {
        while(1); /*创建失败*/
    }
	reset_reason_get(&mangpatio_reset_reason);//获取设备重启的原因
	mangpatio_data_st.send_datalen = 0;
	//mangpatio_data_st.send_st = N808_SEND_INIT;
	/* 平台缓存 */
	mangpatio_recv_data_mutex_create();
    mangpatio_recv_data.data = (uint8_t *)gaoz_fibo_malloc( PATIO_RX_BUF_SIZE );
    if(mangpatio_recv_data.data == NULL)
    {
        while(1); /*创建失败*/
    }
    memset((uint8_t *)mangpatio_recv_data.data, 0x00 , PATIO_RX_BUF_SIZE);
    mangpatio_recv_data.len = 0;
	/*OTA check*/
	//fota_checkversion();
	memset((uint8_t *)&socket_state_2 , 0x00 , sizeof(socket_state_t));
	memset((uint8_t *)&socket_handle_2 , 0x00 , sizeof(socket_handle_s));
	socket_default( &socket_handle_2 );
}

/** ****************************************************************************
* @remarks       void mangpatio_create_task( unsigned portBASE_TYPE uxPriority )
* @brief         808国标任务
* @param[in]     uxPriority 优先级
* @param[out]    无
* @return        无
* @attention     在系统启动后进行创建
*******************************************************************************/
void mangpatio_create_task( unsigned portBASE_TYPE uxPriority )
{
	mangpatio_net_run_para_init();
	mangpatio_socket_semaphore_create();
	mangpatio_net_patiores_queue_create();
	mangpatio_net_devres_queue_create();
	mangpatio_ctlock_queue_create();
	mangpatio_ctlock_res_queue_create();
	mangpatio_upgrade_queue_create();
	gaoz_thread_create(mangpatio_net_st_check_task, "mangpatio_st_check", 1024*4, NULL, OSI_PRIORITY_NORMAL);
	gaoz_thread_create(mangpatio_send_data_handle_task, MANG_SENDTASK_NAME, 1024*10, NULL, uxPriority);
    gaoz_thread_create(mangpatio_recv_data_handle_task, MANG_RECVTASK_NAME, 1024*10, NULL, uxPriority);
}

